solve this problem by using methods to break the problem up. Write a program tha
ID: 3544530 • Letter: S
Question
solve this problem by using methods to break the problem up.
Write a program that throws a dice until certain number appears a given number of times in a row. A rondom dice number can be generated with following code: int diceFaceNumber = (int)(Math.random() *6+1). The game should prompt the client for a dice face number he would like to appear in row. The game then throws the dice until the dice face number appears that many times in row. the game reports the number of throws it took to get that dice face number to appear the requested number of time in row. Allow the client to repeat the game as many times as he wishes. Use several method that guarantees proper input, a private method that does the computing, and a private method that prints the output.
Commas can be insereted by using the follwing code. Declare an instance of the class DecimalFormat in the same way a Scanner instance is created. Send a String to the class showing where you would like the commas. Use the method format () with integer argument. A String is returned with commas inserted.
importjava.text.DecimalFormat:// this imports the DecimalFormat class
DecimalFormat numberFormat= new DecimalFormat ( " ##,###,###,###");// creates an instance of DecimalFormat
String number= numberFormat.format(numberOfThrows);//number is a String representing numberOfThrows with commas
the output:
Please enter the dice face number that you would like to be repeated.
5
Please enter the number of times that you would like 5 to appear in a row.
10
It took 272,865,832 throws to get the number 5 to appear 10 times in a row.
Would you like to play the game again?
Please enter (yes/no)
yes
Please enter the dice 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 342,657,442 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)
Explanation / Answer
import java.util.*;
public class test20 {
public static void main(String[] args) {
String choice="yes";
while(choice=="yes")
{
System.out.println("Please enter the dice face number that you would like to be repeated.:");
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
int n=Integer.parseInt(str);
System.out.println("Please enter the number of times that you would like "+n+" to appear in a row.");
Scanner sc1=new Scanner(System.in);
str=sc1.nextLine();
int r=Integer.parseInt(str);
Random ran = new Random();
int count1=0,count2=0;
int x =1+(int)(Math.random() *6);count2++;
if(r==1&&x==n)
count1++;
while(count1!=r)
{
int x1 =1+(int)(Math.random() *6);
if(x1==x&&x==n)
count1++;
else
{count1=0;x=x1;}
count2++;
}
System.out.println("It took "+count2+" throws to get the number "+n+" to appear "+r+" times in a row.");
System.out.println("Would you like to play the game again?");
System.out.println("Please enter (yes/no)");
Scanner sc2=new Scanner(System.in);
String ch=sc2.nextLine();
if(choice==ch)
continue;
else
break;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.