Question
Write a program to guess a number from 0 to 999, to the following specifications. Tell the user to think of a number from 0
Write a program to guess a number from 0 to 999, to the following specifications.
Tell the user to think of a number from 0 to 999. Note that the computer is guessing the number, the user just thinking of one.
Always start with a lower bound of 0 and an upper bound of 999.
As long as the user inputs a string starting with 'l' or 'm'
The next guess is always the average of the upper bound and the lower bound, so the first guess is always 499.
Prompt the user in the following format (changing 499 to another guess each time) and then input a string.
Is it 499? (Enter y if it is, l if your number is less, or m if your number is more.)
As long as the user does not enter 'y', 'l' or 'm', prompt them with Enter y, lowercase L, or m only! Try again: and input another string.
If the user inputs a string starting with 'l' (or 'L'), change the upper bound.
If the user inputs a string starting with 'm' (or 'M'), change the lower bound.
The next guess is the average of the upper bound and the lower bound.
Keep count of the number of guesses.
Report the number of guesses made and the correct guess.
Sample run (user input in red):
Think of a number from 0 to 999. Is it 499? (Enter y if it is, l if your number is less, or m if your number is more.) n Enter y, lowercase L, or m only! Try again: l Is it 249? (Enter y if it is, l if your number is less, or m if your number is more.) m Is it 374? (Enter y if it is, l if your number is less, or m if your number is more.) l Is it 311? (Enter y if it is, l if your number is less, or m if your number is more.) y I guessed your number, 311 in 4 guesses.
I need help having java output random number as well as continuing the while loop
this is what i have so far
String y,l,m;
int lower=0;
int upper=999;
int count=0;
int guess=0;
int guessingnum;
boolean right = false;
System.out.println("Think of a number between 0 and 999");
System.out.println("Is it 499? (Enter y if it is, l if your number is less, "
+ "or m if your number is more.)");
guess=sc.next().charAt(0);
count++;
while (right=false){
}
if(guess=='y'){
right=true;
System.out.println("I guessed your number," + guess + " in " + count + " tries");
}
else if(guess=='m'){
guess= (lower + upper)/2;
System.out.println("is it " + guess + "?" + "(Enter y if it is, l if your number is less, or m if your number is more.)");
guess=sc.next().charAt(0);
count++;
}
else if (guess=='l'){
guess=(upper + lower)/2;
System.out.println("is it " + guess + "?" + "(Enter y if it is, l if your number is less, or m if your number is more.)");
guess=sc.next().charAt(0);
count++;
}
else if(guess=='n'){
System.out.println("Enter y, lowercase L, or m only! Try again:");
return;
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started