【问题标题】:Two Dice 2 Java Issue [duplicate]两个骰子 2 Java 问题 [重复]
【发布时间】: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 类提供一个像样的 equalshashCode 方法,否则 equals 方法将失败。除非你也比较

标签: java


【解决方案1】:

Die是自定义类型(不是intlong等原始类型),所以为了告诉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);
        }
    }

此外,如上所示,最好在 implement Comparable 时覆盖 equals()hashcode()

【讨论】:

  • 还不如给类一个equals和hashCode方法,这样比较结果就和函数相等和hashCode测试一致了。即,如果 compareTo 返回 0,则合约规定这两个类应该相等并且具有相同的哈希码。
  • 好的,正在更新...
【解决方案2】:

您只需要在每个 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 只会为您提供用于生成数字而不是实际数字的种子

【讨论】:

    【解决方案3】:

    您可以像 javaguy 的答案那样实现可比较,或者您可以使用 firstDie.getValue == secondDie.getValuefirstDie.getValue &gt; secondDie.getValue 等等,这样您就可以比较 ints 而不是 objects。它看起来像这样:

    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);
            }
    
    }
    

    【讨论】:

    • 谢谢大家的帮助!在所有帮助下很容易弄清楚并得到照顾。太困扰我了。
    • 欢迎您:)
    猜你喜欢
    • 2014-12-06
    • 2021-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 2020-06-06
    • 2017-02-25
    相关资源
    最近更新 更多