Programming Review 4 Objectives: ? Use of Random object to generate pseudo-rando
ID: 643111 • Letter: P
Question
Programming Review 4 Objectives: ? Use of Random object to generate pseudo-random numbers to simulate an event; ? Use of looping structures to force appropriate input; ? String comparisons; ? Use of print f for formatted output; ? Use of Scanner class to implement interactive program. Recall that a standard die has six sides with pips on each side numbering 1 through 6. In this assignment you are to simulate rolling 1 or more dice until a specified number of pips shows up (sum of all the top values on the dice). Your program should do the following: 1. Ask the user to Input the number of dice to throw and then read any value n from 2 to 10. It should continue to ask for input until an integer in the range 2 to 10 inclusive is entered. If a value other than this is entered. the program should state that it is invalid, remind the user of the appropriate range, and then request another. 2. It should then ask the user to enter another Integer m which is In the range from it to 6n. Again it should request input until a valid number Is entered, each time reminding the user what range of number is valid. 3. It should then continue to simulate throwing it dice until the number in occurs, counting the number of throws and then print out how many throws were done to find tn. 4. After completing steps 1 through 3, it should ask the user if another trial is desired and accept Yes or No disregarding the case of the letters in these two answers. Any other string should be reported as invalid and requests for an answer repeated until one of the four legitimate answers is given. Continue hack at 1. above until NO or no is entered, in which case the program terminates. Requirements: ? Your program must be well structured and commented. ? All output must be appropriately labeled or described. ? All requests for input should clearly define what constitutes legitimate input. ? You MUST simulate the roll of n dice with a method with parameters consisting of a Random object and an integer representing the number of dice to be thrown. ? You MUST use printf to report the number of rolls appropriately Identified.Explanation / Answer
import java.util.Random;
import java.util.Scanner;
public class RandomDice {
public static void main(String args[]){
Scanner in = new Scanner(System.in);
Random r = new Random();
String choice = "";
int numDice, value, numRolls, sum;
while(true){
System.out.printf("Enter number of dice, an integer in [2, 10]: ");
numDice = in.nextInt();
while(numDice > 10 || numDice < 2){
System.out.printf("Must be in [2, 10]. Reenter: ");
numDice = in.nextInt();
}
System.out.printf("Enter a value in [%d, %d] to hit when rolling: ", numDice, (6 * numDice));
value = in.nextInt();
while(value < numDice || value > (6 * numDice)){
System.out.printf("Must be in [%d, %d]. Reenter: ", numDice, (6 * numDice));
value = in.nextInt();
}
numRolls = 0;
while(true){
sum = 0;
for(int i = 0; i < numDice; ++i){
sum += r.nextInt(6) + 1;
}
numRolls++;
if(sum == value){
break;
}
}
System.out.printf("With %d dice, it took %d rolls to find %d", numDice, numRolls, value);
System.out.printf(" Would you like to roll more dice? (Yes or No)?: ");
choice = in.nextLine();
choice = in.nextLine();
while(!choice.equalsIgnoreCase("Yes") && !choice.equalsIgnoreCase("No")){
System.out.printf("Enter Yes or No: ");
choice = in.nextLine();
}
if(choice.equalsIgnoreCase("No")){
break;
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.