Write a Java class that will play a simple guessing game with the user of your p
ID: 3755446 • Letter: W
Question
Write a Java class that will play a simple guessing game with the user of your program. Your program begins by describing the rules of the game Then, when playing begins, the program will come up with a number between 0 and 100, and the user will try to guess that number. Each time the user guesses, the program w te the user whether the target number is higher or lower than the guess (but no further information is given). When the user eventually guesses the actual number, the program should let the user know, and also display the number of guesses that the user needed. To simulate a random guess, you should generate a random number using Math.randomO or the Random class. Sample Run: Hello, are you ready to play the hi-lo game?... [include instructions] I am thinking of a number between 0 and 100. Enter a guess: 87 Nope! My number is lower than 87 Enter a guess: 45 No, my number is higher than 45 Enter a guess: 55 Wow! You won in 3 guesses! OPTIONAL: ua a top inlo the prot anytime s n h user inExplanation / Answer
import java.util.Scanner;
public class Guess {
public void process()
{
Scanner sc=new Scanner(System.in);
int randomInt = (int)(100.0 * Math.random());
//System.out.println(randomInt);
int count=0,num;
System.out.println("Hello ,are you ready to play the hi-lo game?");
System.out.println("1.Number is Between 0 to 100");
System.out.println("2.you can guess until number is not match and your attempt also on board ");
do {
System.out.println("Enter a guess ");
num=sc.nextInt();
if(num>randomInt)
{
System.out.println("Nope! My number is Lower Than "+num);
count=count+1;
}
else if(num<randomInt)
{
System.out.println("Nope! My number is higher Than "+num);
count=count+1;
}
else {
count=count+1;
System.out.println("Wow! You won in "+count+" guesses!");
break;
}
}while(true);
}
public static void main(String[] args) {
Guess g=new Guess();
g.process();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.