随堂练习——猜生日

 1   Scanner sc=new Scanner(System.in);
 2         System.out.println("请猜一下我的生日(1~31):");
 3         int a=sc.nextInt();
 4         int b=15;        //可以用final来把b定义成常量
 5         while(b!=a)
 6         {
 7             if(a>b&&a<=31)
 8             {
 9             System.out.println("生日猜大了,请继续猜:");
10              a=sc.nextInt();
11             }
12             else if(a>=1&&a<b)
13             {
14                 System.out.println("生日猜小了,请继续猜:");
15                 a=sc.nextInt();
16             }
17                 else
18                 {
19                     System.out.println("请输入所给的范围:");
20                     a=sc.nextInt();
21                 }                         
22         }        
23         System.out.println("猜对了!");
sc.close(); //这表示清除内存

则运行结果为:

原文地址:https://www.cnblogs.com/hanazawalove/p/5234194.html