1 /* 2 猜数字游戏 3 */ 4 import java.util.Scanner; 5 6 class GuessNumber{ 7 public static void main(String[] args){ 8 9 //生成随机数 10 int num = (int)(Math.random()*100)+1; 11 12 //比较 13 while(true){ 14 System.out.println("请输入你猜的数据(1--100)"); 15 Scanner sc = new Scanner(System.in); 16 int guessNum = sc.nextInt(); 17 18 if(guessNum > num){ 19 System.out.println("你猜的数据:"+guessNum+"大了"); 20 }else if (guessNum < num){ 21 System.out.println("你猜的数据:"+guessNum+"小了"); 22 }else { 23 System.out.println("恭喜你,猜对了!"); 24 break; 25 } 26 } 27 } 28 }