Christopher Huff Christopher Huff - 1 year ago 121
Java Question

Two Dice 2 Java Issue

Alright, My issue is that the 2nd if and 3rd if statement is throwing up an Operator '<' '>' is undefined.

It should be two int values and show be able to do lesser or greater, not sure why it's not working on my end. Here are the two codes:

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

}


}

And:

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

Answer Source

you just need to add a get value after each of the Dice here is the fixed code

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

}

you need to add it after all of them because firstdie and second die only get you the seed used to generate the number not the actual number

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download