public class Test {
public static void main(String[] args) {
System.out.println("Welcome to Siddharth's number guessing game. \nThink of a number between 1 and 100 and press 1 to continue");
Scanner x= new Scanner(System.in);
if (x.nextInt()==1)
{
int high=100;
int low=0;
int guess=(high+low)/2;
System.out.println("Is your number "+ guess+"? Press 0 if yes, press 1 if your number is higher than this number or press 2 if your number is lower than this number!");
Scanner y= new Scanner(System.in);
int ans=y.nextInt();
while(ans!=0)
{
if (ans==1)
{
low=ans;
guess=(high+low)/2;
System.out.println("Is your number "+ guess+"? Press 0 if yes, press 1 if your number is higher than this number or press 2 if your number is lower than this number!");
Scanner y1= new Scanner(System.in);
ans=y1.nextInt();
}
else if (ans==2)
{
high=ans;
guess=(high+low)/2;
System.out.println("Is your number "+ guess+"? Press 0 if yes, press 1 if your number is higher than this number or press 2 if your number is lower than this number!");
Scanner y2= new Scanner(System.in);
ans=y2.nextInt();
}
}
System.out.println("The number you thought is"+guess+"! Thanks for playing!");
}
else
{
System.out.println("No problem. Restart the program and press 1 when ready!");
}
}
}
Я подумал, что номер 22, и вошел 2, когда он спросил меня, если мой номер равен 50. Затем он сразу же пошел в 1. Что бы я ни вводил, он показывает 1 как вместо 50, 25, 12 и т. д. Я сделал то же самое в Python и C, и они отлично работают.Java number guessing game weird output
Почему вы создаете все эти 'Scanners'? – Idos