簡體   English   中英

java-equals函數無法正常工作

[英]java - equals function wont work

注意:我的英語不是最好的,所以請不要介意過多的語法錯誤。

嘿,這里的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;
    }
}

如果您知道出了什么問題,請回復此帖子。

如果您想使其正常工作,則需要像這樣進行比較:

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

或(java 7 ++)

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

timeAtomicInteger的實例, times2.getTime()是一個inttimes2.getTime()轉換為Integer。

那是兩個不同的類。

因此,要解決該問題,您需要簡單地通過times.get()將兩者都帶入同一類型,這將產生一個int值。

您應該覆蓋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;

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM