Write a game program that prints a chart to the screen showing the randomness of
ID: 3641175 • Letter: W
Question
Write a game program that prints a chart to the screen showing the randomness of a dice. First the game prompts the user for a number of die throws he would like. The game then throws that die that many times. The game then prints a chart showing a line of asterisks for each die number, indicating the number of times the die landed with that face number of top. The chart should look different everytime it runs.A sample output:
Enter the number of times you would like to throw the dice: 20
The chart showing 20 throws of the dice:
1 ****
2 ****
3 **
4 ***
5 *****
6 **
Heres what i have so far:
System.out.println("Please enter the number of times that");
System.out.println("you would like to throw the dice: ");
Scanner input = new Scanner (System.in);
numberOfTimes = input.nextInt();
System.out.println("The chart showing " + numberOfTimes + " throws of the dice:");
while(dieResults >= numberOfTimes) //here im telling it that the number of throws cannot be more than the number the user told it.
{
dieResults = (int)(6.0*Math.random()) +1; //get random number
dieResults++; //keepts getting random numbers until while condition met.
System.out.println(dieResults); //print out results.
}
Now, how do i make the chart and put an asterisks for each times its one of the numbers. I was thinking that maybe its a lot of If statements. For example If diesults == 1 then system.out.println ("*"); i tried that but it doesn't work. so can someone please help not only write the code for me but explain each step.
And it needs to use the math.random() and not import a random number generator
Explanation / Answer
import java.util.Scanner;
public class DiceThrowingChart {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
int numberOfTimes;
int [] diceValues = new int[6]; //Use array to store the number of appearance of each value
System.out.println("Please enter the number of times that");
System.out.println("you would like to throw the dice: ");
numberOfTimes = input.nextInt();
System.out.println("The chart showing " + numberOfTimes + " throws of the dice:");
//get random number
while(numberOfTimes > 0) {
//(result 0-5 for easier storing to the array diceValues)
int result = (int)(6.0*Math.random());
diceValues[result]++;
numberOfTimes--;
}
//Print the chart
for (int i = 0; i < diceValues.length; i++) {
System.out.print(i + 1 + " ");
for (int astr = 0; astr < diceValues[i]; astr++) {
System.out.print("*");
}
System.out.println();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.