1 package study; 2 3 import java.util.Scanner; 4 5 /** 6 * 猜数字小游戏 7 * 8 * @author carry 9 * 10 */ 11 public class GuessNumberDemo { 12 public static void main(String[] args) { 13 int number = (int) (Math.random() * 100) + 1; 14 System.out.println(number); 15 while (true) { 16 //System.out.println("请输入您猜到的数字:"); 17 @SuppressWarnings("resource") 18 String line = new Scanner(System.in).nextLine(); 19 int value = Integer.parseInt(line); 20 if (number > value) { 21 System.out.println("您猜小了"); 22 } 23 if (number == value) { 24 System.out.println("您猜对了"); 25 break; 26 } 27 if (number < value) { 28 System.out.println("您猜大了"); 29 } 30 } 31 } 32 }