Java cube with unexpected random number
I wrote a simple Java program to display the results of 20 rolls of dice on the console. The results I get are listed below:
3
1
java.util.Random@62efae3b
1
5
4
1
java.util.Random@62efae3b
1
java.util.Random@62efae3b
java.util.Random@62efae3b
1
6
java.util.Random@62efae3b
1
java.util.Random@62efae3b
java.util.Random@62efae3b
1
2
3
3
When I run it several times, the line after the "@" is different, but mostly in the same format. What have I done wrong?
Here is the code:
import java.util.Random;
public class QiProb3 {
public static void main(String[] args) {
Random diceNumber = new Random();
for (int count = 0; count <= 20; count++) {
if ((diceNumber.nextInt(6) + 1) == 1) {
System.out.println("1");
} else if ((diceNumber.nextInt(6) + 1) == 2) {
System.out.println("2");
} else if ((diceNumber.nextInt(6) + 1) == 3) {
System.out.println("3");
} else if ((diceNumber.nextInt(6) + 1) == 4) {
System.out.println("4");
} else if ((diceNumber.nextInt(6) + 1) == 5) {
System.out.println("5");
} else if ((diceNumber.nextInt(6) + 1) == 6) {
System.out.println("6");
} else {
System.out.println(diceNumber);
}
}
}
}
source to share
else {
System.out.println(diceNumber);
}
You print the address diceNumber
by calling its toString()
default function in the else clause.
This is why you getjava.util.Random@62efae3b
the more important issue is why it ends up in the 'else' clause, I believe it is not your intention.
Note: In this question, if/else if
a new number is generated for each sentence , so the code does end up in the final sentence else
.
What should you do:
for (int count = 0; count < 20; count++) {
int rollValue = diceNumber.nextInt(6) + 1;
if (rollValue == 1) {
System.out.println("1");
} else if (rollValue == 2) {
System.out.println("2");
} else if (rollValue == 3) {
System.out.println("3");
} else if (rollValue == 4) {
System.out.println("4");
} else if (rollValue == 5) {
System.out.println("5");
} else if (rollValue == 6) {
System.out.println("6");
} else {
// This else is now redundant
System.out.println(diceNumber);
}
}
or a more direct method:
// count < 20 instead of count <= 20
for (int count = 0; count < 20; count++) {
int rollValue = diceNumber.nextInt(6) + 1;
System.out.println(rollValue);
}
Credit goes to "Elliot Frisch" for realizing that the loop runs 21 times instead of 20.
source to share
You flipped
With each one, if
you roll the dice repeatedly. Save this value and test it.
Random diceNumber = new Random();
for (int count = 0; count <= 20; count++) {
int roll = diceNumber.nextInt(6) + 1;
if (roll == 1) {
System.out.println("1");
} else if (roll == 2) {
System.out.println("2");
} else if (roll == 3) {
System.out.println("3");
} else if (roll == 4) {
System.out.println("4");
} else if (roll == 5) {
System.out.println("5");
} else if (roll == 6) {
System.out.println("6");
} else {
System.out.println("RNG Error: " + diceNumber);
}
}
This could be more concise
Your posted code can be shortened like
for (int count = 0; count <= 20; count++) {
int roll = diceNumber.nextInt(6) + 1;
System.out.println(roll);
}
Note
Plus, you get 21 rolls using the above test <= 20
.
source to share
You can do it without the big if-else ladder:
int x = 0;
int i = 0;
while(i < 20){
x = (int)(Math.random() * 7);
if(x != 0)
{
System.out.println((int)Math.floor(x));
i++;
}
}
Math.random () gets a value between 0 and 1, and this value is multiplied by 7. If the dice go to zero, skip the roll and make another. The Math.floor () value will round the decimal value to the nearest whole number (if product = 6.2, then the roll yield will be 6).
source to share