【发布时间】:2016-11-26 17:50:16
【问题描述】:
好吧,我的问题是第二个 if 和第三个 if 语句抛出了一个运算符 '' 是未定义的。
它应该是两个 int 值并显示能够做更少或更大,不知道为什么它不适合我。这是两个代码:
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; } }
【问题讨论】:
-
模具不是整数。您可能想将
firstDie.getValue()与 secondDie 进行比较。您的==也会失败,因为它会检查引用是否相等。除非你给你的 Die 类提供一个像样的equals和hashCode方法,否则 equals 方法将失败。除非你也比较值。
标签: java