用java编写。模拟一个简单的测试打字速度的游戏

请高手帮我看看我的程序哪里出现错误了,输出打字速度时出错
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.*;
public class TestTypeSpeed extends JFrame{
JTextArea ta1; //显示开始时间JTextArea ta2; //显示结束时间JTextArea ta3; //显示打字速度JTextField tf; //打字区
Calendar c1=Calendar.getInstance(),c2=Calendar.getInstance();
Date d;SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");double time;
public static void main(String[] args) {
new TestTypeSpeed();
}
public TestTypeSpeed() {
initCenter();initEast();initJFrame();
}
private void initJFrame() {
this.setBounds(200,200, 400, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
private void initCenter() {
JPanel p1 = new JPanel();
p1.setLayout(new BorderLayout());
JLabel l1 = new JLabel("测试打字速度");
l1.setBackground(Color.YELLOW);
p1.add(l1,BorderLayout.NORTH);
JPanel p3 = new JPanel();
p3.setLayout(new GridLayout(3,2));
JButton b1 = new JButton("开始");
p3.add(b1);
Monitor8 h = new Monitor8();
b1.addActionListener(h);
ta1= new JTextArea();
p3.add(ta1);
JButton b2 = new JButton("结束");
p3.add(b2);
b2.addActionListener(h);
ta2 = new JTextArea();
p3.add(ta2);
p1.add(p3,BorderLayout.CENTER);
JButton b3 = new JButton("您的打字速度为");
p3.add(b3);
b3.addActionListener(h);
ta3 = new JTextArea();
p3.add(ta3);
this.add(p1);
}
private void initEast(){
JPanel p2 = new JPanel();
p2.setLayout(new BorderLayout());
this.add(p2,BorderLayout.EAST);
JLabel ta3 = new JLabel("打字区");
p2.add(ta3,BorderLayout.NORTH);
tf = new JTextField(10);
p2.add(tf,BorderLayout.CENTER);
this.add(p2,BorderLayout.EAST);
}

private String getTheTime(Calendar c){
d = c.getTime();
String date = sdf.format(d);
return date;
}
private double getTimeInterval(){
double second = c2.get(Calendar.SECOND) - c1.get(Calendar.SECOND);
double minute = c2.get(Calendar.MINUTE) - c2.get(Calendar.MINUTE);
double hour = c2.get(Calendar.HOUR_OF_DAY) - c1.get(Calendar.HOUR_OF_DAY);
time = hour*3600+minute*60+second;
return time;
}
private class Monitor8 implements ActionListener{
public void actionPerformed(ActionEvent e) {
String comn = e.getActionCommand();
if(comn.equals("开始")){
Calendar c1 = Calendar.getInstance();
ta1.append(getTheTime(c1));
}
if(comn.equals("结束")){
Calendar c2 = Calendar.getInstance();
ta2.append(getTheTime(c2));
}
if(comn.equals("您的打字速度为")){
String tt = tf.getText();
int word = tt.length();
ta3.append(tt.length()/getTimeInterval()+"字每秒");
}
}
}
}

时间差没有得到。。。
double second = c2.get(Calendar.SECOND) - c1.get(Calendar.SECOND);
double minute = c2.get(Calendar.MINUTE) - c2.get(Calendar.MINUTE);
double hour = c2.get(Calendar.HOUR_OF_DAY) - c1.get(Calendar.HOUR_OF_DAY);
有错追问

然后呢?我怎么改啊?我没看出错来啊亲!

追答

我没学过这个class你最好把那两个时间分解了!我输出了这个时间差是0

温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-03-27
因为你的C1 C2是同时NEW出来的 所以算不出来结果 C1得到示例放在TYPE前 C2得到实例放大TYPE结束 再取差值
相似回答