2017-08-08 76 views
-1

注意:我的英語不是最好的,所以請不要介意太多的語法錯誤。java - 等於功能不會工作

嘿,那裏,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; 
    } 
} 

如果你知道什麼是錯的這個帖子請回復。

+1

你沒有覆蓋equals方法。看看[這裏](https://stackoverflow.com/questions/2265503/why-do-i-need-to-override-the-equals-and-hashcode-methods-in-java) – XtremeBaumer

+1

這不是真的有關對於這個問題,但在Java類中通常是大寫的,所以times2應該是Times2。 – Acontz

回答

2

如果你想爲它工作,你需要比較像這樣:

times.get().equals(times2.getTimes()) 

或(Java 7中++)

Objects.equals(times.get(), times2.getTimes()) 

timeAtomicInteger一個實例,times2.getTime()int ,鑄造到Integer。

這些是兩個不同的類。

所以要解決它,你需要把兩者都帶到相同的類型,只需通過times.get(),它將產生一個int。

+0

謝謝,但它會與times2.getTimes();太? –

+0

您需要將AtomicInteger轉換爲int,然後進行比較。我已經更新了答案。 – Beri

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; 

} 
+0

重寫它的times2類權利? –

+0

重寫它在原子整數? –

+0

是的,需要重寫Times2類 –