注意:我的英语不是最好的,所以请不要太在意语法错误。
嘿那里,Java Starter在这里,无论如何我正在写我的CPS测试程序,我发现了一个我无法弄清楚的错误。之前可能会问过这个问题,但是我无法使用.equals函数。
代码:
boolean wait = false;
times2 times2 = new times2(0, false);
times2.setTimes(0);
final AtomicInteger times = new AtomicInteger(0);
b1.addActionListener(new ActionListener() {
final AtomicInteger clicks = new AtomicInteger(0);
@SuppressWarnings("unlikely-arg-type")
public void actionPerformed(ActionEvent e) {
System.out.println("Console> startTest;");
// This if condition won't work.
if(times.equals(times2.getTimes())) {
b1.setText("3");
try {
TimeUnit.SECONDS.sleep((long)1.0);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
b1.setText("2");
try {
TimeUnit.SECONDS.sleep((long)1.0);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
b1.setText("1");
try {
TimeUnit.SECONDS.sleep((long)1.0);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
b1.setText("CLICK!");
times.incrementAndGet();
times2.setWait(true);
}else if(!times.equals(times2.getTimes())){
clicks.incrementAndGet();
}
if(times2.getWait() == true) {
try {
TimeUnit.SECONDS.sleep((long)10.0);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
JLabel text2 = new JLabel("");
Font f = new Font(null, Font.BOLD , 0);
Font size = f.deriveFont(45f);
double clicks2 = clicks.get();
double results = clicks2/10;
text2.setText("<html> Your final Results: <html> " + "<html> <br> <html>" + results);
text2.setFont(size);
text2.setPreferredSize(new Dimension(100,100));
JFrame end = new JFrame();
end.setPreferredSize(new Dimension(350,350));
end.setMaximumSize(new Dimension(450,450));
end.setLocationRelativeTo(null);
end.setTitle("RESULTS");
end.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
end.pack();
end.setVisible(true);
end.getContentPane().add(text2, BorderLayout.CENTER);
}
}
});
times2.java:
public class times2 {
private int times;
private boolean wait;
public times2(int times2, boolean wait2) {
this.times = times2;
this.wait = wait2;
}
public int getTimes() {
return times;
}
public void setTimes(int times) {
this.times = times;
}
public boolean isWait() {
return wait;
}
public void setWait(boolean wait) {
this.wait = wait;
}
public boolean getWait() {
// TODO Auto-generated method stub
return false;
}
}
如果您知道什么不对请回复此帖。
答案 0 :(得分:2)
如果你想让它工作,你需要比较如下:
times.get().equals(times2.getTimes())
或(java 7 ++)
Objects.equals(times.get(), times2.getTimes())
time
是AtomicInteger
的实例,times2.getTime()
是int
,已投放到整数。
这是两个不同的类别。
所以要解决它,你需要将两者都带到相同的类型,只需要times.get(),这将生成一个int。
答案 1 :(得分:2)
你应该覆盖equals方法
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
test2 test2 = (test2) o;
if (times != test2.times) return false;
return wait == test2.wait;
}