java第三天小练习(猜数字游戏)

 1 import java.util.Random;
 2 import java.util.Scanner;
 3 
 4 public class DateType {
 5     public static void main(String[] args) {
 6         System.out.println("猜数字游戏开始了");
 7         System.out.println("请输入1-100之间的数字");
 8         //后台生成一个伪随机数范围在1-100;
 9         Random ra = new Random();
10         int usr = ra.nextInt(100)+1;
11         //创建Scanner类对象
12         Scanner sc = new Scanner(System.in);
13         //主程序
14         while(true) {
15             //获取用户的输入
16             int usc = sc.nextInt();
17             //后台随机数和用户输入的数字进行比较
18             if(usc == usr) {
19                 System.out.println("恭喜您,答对了!");
20                 break;
21             }else if(usc>usr){
22                 System.out.println("sorry,您猜大了!");
23             }else {
24                 System.out.println("sorry,您猜小了!");
25             }
26         }        
27     }
28 }

猜你喜欢

转载自www.cnblogs.com/chenguoming666/p/9062692.html