dimanche 21 juin 2015

Java dice roll with unexpected random number

I've written a simple Java program to display the results of 20 dice rolls on the console. The results I'm getting 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 ran it for a few times, the string after "@" is different, but basically 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);
            }
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire