import java.util.Scanner;
public class GuessNumberOneTime {
public static void main(String[] args) {
// TODO 自动生成的方法存根
int number = (int)(Math.random()*101);
Scanner input = new Scanner (System.in);
System.out.println ("Guess a magic numberr between 0 to 100");
int guess = -1; //这一句非常重要,思想额很重要
while(guess != number){
System.out.print("\n Enter your guess: ");
guess = input.nextInt();
if (guess == number)
System.out.println("\nYes, the number is "+number);
else if(guess < number)
System.out.println("\nyour guess is too low");
else
System.out.print(" \nyour guess is too high");
}
}
}
这个代码主要是学习应用循环和选择语句,上面有注释的那一行,经常用到这种方法,要注意咯,其他的没什么复杂的。