Project 5 10th week. Write a game program that throws a die (singular for dice)
ID: 3531988 • Letter: P
Question
Project 5 10th week. Write a game program that throws a die (singular for dice) until a certain number appears a given number of times in a row. The game should first prompt the client for a die face number he would like to appear in a row. The game then throws the die until that die face number appears that many times in a row. The game reports the number of throws it took to get that die face number to appear the requested number of times in a row. Allow the client to repeat the game as many times as she wishes. Use several methods: you might have one for input, one for processing, and one for output. Guarantee inputs are in an acceptable range. A sample run of the game would look like this:
This game throws a die until a certain die face number
appears in a row a certain number of times.
Please enter the die face number that you would like to be repeated. 4
Please enter the number of times that you would like 4 to appear in a row. 10
It took 36,227,900 throws to get the number 4 to appear 10 times in a row.
Would you like to play the game again?
Please enter (yes/no) no
NEED A CODE
Explanation / Answer
import java.util.Scanner;
public class Dies {
Scanner input;
public int dieFaceNumber;
public int numberOfRow;
public int howManyThrows;
public void input()
{
input= new Scanner (System.in);
howManyThrows=0;
System.out.println("This game throws a die until a certain dice face number appears in a row a certain number of times.");
System.out.println("Please enter the dice face number that you would like to be repeated:");
dieFaceNumber = input.nextInt();
System.out.println("Please enter the number of times you would like " + dieFaceNumber + " to appear in a row:");
numberOfRow = input.nextInt();
}
public static void main(String [] args)
{
Scanner input= new Scanner (System.in);
Dies game = new Dies();
while(true)
{
game.input();
game.process();
game.output();
String string1= input.next();
if (!string1.equals("yes")) break;
}
}
public void process()
{
while(true)
{
howManyThrows = (int)(Math.random() *6 + 1);
if (howManyThrows == dieFaceNumber)numberOfRow++;
else break;
}
}
public void output()
{
System.out.println("It took " + howManyThrows + " throws to get the number " + dieFaceNumber + " to appear " + numberOfRow + " in a row.");
System.out.println("Would you like to play the game again?");
System.out.println("Please enter (yes/no)");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.