Write a program using the random number generator number to generate a five-digi
ID: 3545697 • Letter: W
Question
Write a program using the random number generator number to generate a five-digit number. With each guess the computer returns two numbers, the first is the number of digits that are in the proper position and the second is the sum of the correct digits. Allow the client to repeat the game as many times as she wishes. one of the important objectives in these program is to solve the program using methods to break the problem up.
Here is the output:
Using the random number generator, this game creates a
five-digit number. The player guesses the number.
The computer returns two numbers, the first is the number
of digits that are in the proper position and the second
is the sum of the correct digits guessed so far.
The player continues guessing until she guesses the correct number.
originalFiveDigitNumber is 67953
Please enter the number you would like to guess.
12345
You have found 0 digits so far with a sum of 0.
Please enter the number you would like to guess.
23456
You have found 1 digits so far with a sum of 5.
Please enter the number you would like to guess.
34557
You have found 1 digits so far with a sum of 5.
Please enter the number you would like to guess.
45658
You have found 1 digits so far with a sum of 5.
Please enter the number you would like to guess.
56759
You have found 1 digits so far with a sum of 5.
Please enter the number you would like to guess.
67851
You have found 3 digits so far with a sum of 18.
Please enter the number you would like to guess.
67952
You have found 4 digits so far with a sum of 27.
Please enter the number you would like to guess.
67953
It took 8 guess to get the number 67953.
Would you like to play the game again?
Please enter (yes/no)
Explanation / Answer
import java.util.*;
public class game {
int num = 0, ans = 0,cnt = 1,foundDigit = 0;
public void setAns(){
ans = 00000 + (int)(Math.random() * 99999);
}
public void guess(){
Scanner scan = new Scanner(System.in);
while(num!=ans){
System.out.println("Please enter the number you would like to guess.");
num = scan.nextInt();
int getsum = process(num);
if(num!=ans){
System.out.println("You have found " + foundDigit + " digits so far with a sum of " + getsum + ". ");
}else{
System.out.println("It took "+ cnt +" guess to get the number" + ans +".");
}
foundDigit = 0;
cnt++;
}
}
public int process(int nam){
int x = ans,y = 0, comAns = 0,comNam = 0, sumofFoundDigit = 0;
while(y!=5){
comAns = x%10;
comNam = nam%10;
nam = nam/10;
x = x/10;
if(comAns==comNam){
sumofFoundDigit = sumofFoundDigit + comNam;
foundDigit++;
}
y++;
}
return sumofFoundDigit;
}
public void flow(){
game game = new game();
game.setAns();
game.guess();
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
game game = new game();
int y =0;
game.flow();
while(y==0){
System.out.println(" Would you like to play the game again? Please enter (yes/no)");
String choice = scan.next();
if(choice.equals("yes")){
game.flow();
}else if(choice.equals("no")){
System.exit(0);
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.