Clancy Zeng :
import java.util.*;
public class number_guassing_game{
private static int a = 0;
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int number=(int)(Math.random()*100+1);
int count=0;
boolean flag = true;
do {
int a = in.nextInt();
count+=1;
if(a>number) {
System.out.println("smaller!");
}else if(a<number) {
System.out.println("bigger!");
}
}while(a != number);
System.out.println("congratulations!"+" You have guessed "+count+" times!");
}
}
Obviously the answer is 48, but it didn't the loop and print the expression.
Eran :
The loops condition
while(a != number);
doesn't see the local variable into which you read the input:
int a = in.nextInt();
It sees the static variable initialized to 0
that never changes:
private static int a = 0;
Therefore the loop never terminates.
You should change
int a = in.nextInt();
to
a = in.nextInt();
Oh, and there's no reason for a
to be static
. It could be a local variable of the main
method, as long as it is declared outside (and before) the loop.