Home>

.
I want to record the number of times (1 to 6) of rolling dice n times using Java and display the graph with (*).
(Example)
1: ********
2: **
3: ***********
~~~~~~~~~~~~~~~~
I've come to the point where I can output a random number using the Rondom class, but I don't know the future. please tell me.

Current code

import java.util.Scanner;
import java.util.Random;

public class test_1 {
public static void main (String [] args) {
Scanner scan = new Scanner (System.in);
Random rand = new Random ();

System.out.print ("Number of dice rolls>");
int max = scan.nextInt ();

for (int i = 0;i int dice = rand.nextInt (6) + 1;

}
System.out.println ();
}
}

  • Answer # 1

      

    I would like to record the number of times (1-6) of rolling a dice n times using Java and display it as a graph (*).

    I think you can do this by writing it yourself ...
    First, let's record. What would you do if you did it manually?

  • Answer # 2

    I don't know if it works because I haven't tested it, but what about it?

    import java.util.Scanner;
    import java.util.Random;
    public class test_1 {
    public static void main (String [] args) {
    Scanner scan = new Scanner (System.in);
    Random rand = new Random ();
    System.out.print ("Number of dice rolls>");
    int max = scan.nextInt ();
    int diceLog [] = new int [6];
    for (int i = 0;i

    By the way,

    int dice = rand.nextInt (6) + 1;
        System.out.println (dice);
        diceLog [dice-1] = diceLog [dice-1] + 1;


    There is no need to output this part, but if you don't check what the value is, you can't check whether the last graph is correct or not.

Trends