In: Computer Science
Write a guessing game java program (basic program, we didn't study further than loops and files) where the user has to guess a secret number between 1 and 100. After every guess the program tells the user whether their number was too large or too small. At the end the number of tries needed should be printed. It counts only as one try if they input the same number multiple times consecutively.Example of running this program:
Enter your secret number guess: __25__________________________________
Program output:
Your guess of 25 is too large!
Enter your secret number guess: __12__________________________________
Your guess of 12 is too large!
Enter your secret number guess: __6__________________________________
Your guess of 6 is too small!
Enter your secret number guess: __10__________________________________
Congratulations you have guessed the secret number of 10 !!!!!
input code:
output:
code:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
/*make a objects*/
Random r=new Random();
Scanner sc=new Scanner(System.in);
/*declare the variables*/
int input=0,guess=0;
/*generate the number*/
guess=r.nextInt(100)+1;
do
{
/*take user input*/
System.out.print("Enter your secret
number guess:\n__");
input=sc.nextInt();
if(input>guess)
{
/*print this if larger*/
System.out.println("Your guess of
"+input+" is too large!");
}
else if(input<guess)
{
/*print this if smaller*/
System.out.println("Your guess of
"+input+" is too small!");
}
else
{
/*else print this*/
System.out.println("Congratulations
you have guessed the secret number of "+input +"!!!!!");
}
}while(guess!=input);
}
}