2016-11-26 7 views
0

申し訳ありませんが、私の問題は、2番目と3番目のif文がスローアップしているということです。演算子 '<' 'は未定義です。2つのダイス2 Javaの問題

これは2つのint値でなければなりません。なぜなら、それが私の最後ではうまくいかない理由がわからないからです。ここでは、2つのコードは次のとおりです。

public class TwoDice2 { 
public static void main(String[ ] args) { 

    Die firstDie = new Die(); 
    Die secondDie = new Die(); 

    if (firstDie == secondDie) { 
    System.out.println("First die is " + firstDie.getValue()); 
    System.out.println("Next die is " + secondDie.getValue()); 
    System.out.println("The two dice are the same!"); 
    } 

     if (firstDie > secondDie) { 
      System.out.println("First die is " + firstDie.getValue()); 
      System.out.println("Next die is " + secondDie.getValue()); 
      System.out.println("Die One: " + firstDie + " is greater than Die Two: " + secondDie); 
     } 
      if (firstDie < secondDie) { 
       System.out.println("First die is " + firstDie.getValue()); 
       System.out.println("Next die is " + secondDie.getValue()); 
       System.out.println("Die One: " + firstDie + " is less than Die Two: " + secondDie); 
      } 

} 

}

そして:

public class Die { 
private int value; 
private static final int HIGHEST_DIE_VALUE = 6; 
private static final int LOWEST_DIE_VALUE = 1; 

    public Die() { 
     value = ((int)(Math.random() * 100) % HIGHEST_DIE_VALUE + LOWEST_DIE_VALUE); 
} public int getValue() { return value; } } 
+0

ダイはintではありません。あなたは 'firstDie.getValue()'とsecondDieと同じものを比較したいでしょう。また、参照平等をチェックするので '=='も失敗します。また、Dieクラスに適切な 'equals'メソッドと' hashCode'メソッドを与えない限り、equalsメソッドは失敗します。 * values *も比較しない限り。 –

答えて

0

数ではない実際の数

1

Dieがためにそうに、カスタム型(intのようではないプリミティブ型、long、等。)であります2つのオブジェクトがどのように比較できるか(つまり等しいかどうか)をJVMに教えてください。Dieクラスは以下のようにComparableを実装する必要があります(また、副注釈としてを使用してください)。同じ条件を検証するための

ダイスクラス:

public class Die implements Comparable<Die> { 

     private int value; 
     private static final int HIGHEST_DIE_VALUE = 6; 
     private static final int LOWEST_DIE_VALUE = 1; 

     public Die() { 
       value = ((int)(Math.random() * 100)% 
        HIGHEST_DIE_VALUE + LOWEST_DIE_VALUE); 
     } 

     public int getValue() { 
      return value; 
     } 

     @Override 
     public int compareTo(Die o) { 
      if(this.value < o.getValue()) { 
       return -1; 
      } else if(this.value > o.getValue()) { 
       return 1; 
      } 
      return 0; 
     } 

     @Override 
     public boolean equals(Object obj) { 
      Die die = (Die)obj; 
      if (this.getValue() == die.getValue()) { 
      return true; 
      } else { 
      return false; 
      } 
     } 

     @Override 
     public int hashCode() { 
     return value; 
     } 
    } 

は使用方法:

public static void main(String[] args) { 
     Die firstDie = new Die(); 
     Die secondDie = new Die(); 

     if (firstDie.compareTo(secondDie) == 0) { 
      System.out.println("First die is " + firstDie.getValue()); 
      System.out.println("Next die is " + secondDie.getValue()); 
      System.out.println("The two dice are the same!"); 
     } else if (firstDie.compareTo(secondDie) > 0) { 
      System.out.println("First die is " + firstDie.getValue()); 
      System.out.println("Next die is " + secondDie.getValue()); 
      System.out.println("Die One: " + firstDie + " 
       is greater than Die Two: " + secondDie); 
     } else if (firstDie.compareTo(secondDie) < 0) { 
      System.out.println("First die is " + firstDie.getValue()); 
      System.out.println("Next die is " + secondDie.getValue()); 
      System.out.println("Die One: " + firstDie + " is 
        less than Die Two: " + secondDie); 
     } 
    } 

上記のようにまた、上書きすることがベストプラクティスですequals() & hashcode()いつでもimplementComparable。あなたはちょうどここダイス のそれぞれは、あなたがfirstdieと第二のダイが唯一のあなたに使用されたシードを得るために、それらのすべての後に、それを追加する必要があり、固定コード

public class TwoDice2 { 
public static void main(String[ ] args) { 

Die firstDie = new Die(); 
Die secondDie = new Die(); 

if (firstDie.getValue() == secondDie.getValue()) { 
System.out.println("First die is " + firstDie.getValue()); 
System.out.println("Next die is " + secondDie.getValue()); 
System.out.println("The two dice are the same!"); 
} 

    if (firstDie.getValue() > secondDie.getValue()) { 
     System.out.println("First die is " + firstDie.getValue()); 
     System.out.println("Next die is " + secondDie.getValue()); 
     System.out.println("Die One: " + firstDie.getValue() + " is greater than Die Two: " + secondDie.getValue()); 
    } 
     if (firstDie.getValue() < secondDie.getValue()) { 
      System.out.println("First die is " + firstDie.getValue()); 
      System.out.println("Next die is " + secondDie.getValue()); 
      System.out.println("Die One: " + firstDie.getValue() + " is less than Die Two: " + secondDie.getValue()); 
     } 

} 

後にGET値を追加する必要が

+0

また、equalsとhashCodeメソッドをクラスに渡して、比較結果が関数の等価性とhashCodeテストと一致するようにすることもできます。 compareToが0を返す場合、コントラクトは2つのクラスが等しくなければならず、同じハッシュコードを持つと規定します。 –

+0

確かに、更新しています... – developer

0

を生成するために、あなたはjavaguyの答えのようにComparableを実装することもできますし、firstDie.getValue == secondDie.getValuefirstDie.getValue > secondDie.getValueを使用することができますので、上のあなたの代わりにobject秒のint Sを比較しているので。

public class TwoDice2 { 
public static void main(String[ ] args) { 

Die firstDie = new Die(); 
Die secondDie = new Die(); 

if (firstDie.getValue() == secondDie.getValue()) { 
System.out.println("First die is " + firstDie.getValue()); 
System.out.println("Next die is " + secondDie.getValue()); 
System.out.println("The two dice are the same!"); 
} 

    if (firstDie.getValue() > secondDie.getValue()) { 
     System.out.println("First die is " + firstDie.getValue()); 
     System.out.println("Next die is " + secondDie.getValue()); 
     System.out.println("Die One: " + firstDie + " is greater than Die Two: " + secondDie); 
    } 
     if (firstDie.getValue() < secondDie.getValue()) { 
      System.out.println("First die is " + firstDie.getValue()); 
      System.out.println("Next die is " + secondDie.getValue()); 
      System.out.println("Die One: " + firstDie + " is less than Die Two: " + secondDie); 
     } 

} 
+0

ありがとうございました!すべての助けを借りて把握して、それを手に入れました。私をそんなに悩ませていた。 –

+0

あなたの歓迎:) – cat16