import java.util.Random; import java.util.Scanner; public class GuessNumber { public static void main(String[] args) { Random rand = new Random(); int number = rand.nextInt(100);//产生一个0-100间的随机数 Scanner in = new Scanner(System.in);//获取用户的输入 System.out.println("请输入一个数:"); while(true){ int data = in.nextInt();//得到用户输入的数字 if(data>number){ System.out.println("大了,请继续输入:"); }else if(data<number){ System.out.println("小了,请继续输入:"); }else{ System.out.println("恭喜你猜对了!!"); break; } } } }