import java.util.Scanner;
public class GuessYourNumber {
public static void main(String[] args) {
final int MIN = 1;
final int MAX = 100;
Scanner in = new Scanner(System.in);
int min = MIN, max = MAX, guess = MAX/2;
char answer = '.';
boolean found = false;
System.out.println("Choose a number between "+MIN+" and "+MAX+".");
System.out.println("Press a key to continue...");
in.nextLine();
while (!found) {
System.out.print("Is it "+guess+" (answer < > = )? ");
answer = in.nextLine().charAt(0);
if (answer=='<') { // lower
max = guess;
guess = (min+guess)/2;
} else if (answer=='>') { // higher
min = guess+1;
guess = (guess+max)/2;
} else if (answer=='=') { // correct
System.out.println("Great! The secret number is "+guess+".");
found = true;
} // end if
} // end while
} // end main()
} // end class GuessYourNumber
In what language?
visual studio sir
Write a program that asks the user to choose a number between 1 and 100, and then finds it.
?The program can ask guesses.
?The user answers by ‘<‘, ‘>’, or ‘=’, representing “too small”, “too large”, or “correct”,
Ex)
Choose a number between 1 and 100.
Press a key to continue. . .// system(“pause”);
// The secret number is 35.
Is it 50? >// 50 > 35
Is it 25? / 25 < 35
Is it 37? >// 37 > 35
Is it 31? / 31 < 35
Is it 34? / 34 < 35
Is it 35? =// correct answer
Great! The secret number is 35.
Thank you very much!