好吧,我的问题是第二个if和3rd 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; } }
答案 0 :(得分:1)
Die
是一个自定义类型(不是int
,long
等基本类型。),所以为了告诉JVM两个Die
对象如何能够比较(即,它们相等或不相等),您的Die
班级应实施Comparable
,如下所示(另外,作为旁注,使用else if
用于验证相同的条件)
模具类:
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()
&amp;只要您hashcode()
implement
,就会Comparable
。
答案 1 :(得分:0)
你只需要在每个Dice之后添加一个get值 这是固定代码
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());
}
}
你需要在所有这些之后添加它,因为firstdie和second die只能获得用于生成数字而不是实际数字的种子
答案 2 :(得分:0)
您可以像javaguy的答案那样实现可比性,或者您可以使用firstDie.getValue == secondDie.getValue
和firstDie.getValue > secondDie.getValue
等等,这样您就可以比较int
而不是object
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);
}
}