复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("猜数游戏开始,友情提醒你只有10次机会。"); System.out.println("请输入你猜的数字1~100"); System.out.println("输入exit退出游戏。"); Random ran = new Random(); //随机生成一个1~100的数 int a = ran.nextInt(100) + 1; int b = 0; //循环猜数 while (true) { //判断输入的是不是数字或者是exit if (!sc.hasNextInt()) { String st = sc.nextLine(); //退出 if ("exit".equals(st)) { System.out.println("退出游戏。。。"); break; } else { System.out.println("请输入数字:"); continue; } } //接收输入的数字 int i = sc.nextInt(); //判断输入的数字在不在1~100这个范围内 if (i > 100 || i < 1) { System.out.println("请输入1~100的数字"); continue; } //记录猜数的次数 ++b; //对猜的数字进行提醒 if (i == a && b == 1) { System.out.println("恭喜你一次就猜对了,最后的分:100分"); break; } else if (b > 10) { System.out.println("对不起你猜错了,已经没有次数了!"); break; } else if (i > a) { System.out.println("你猜大了,再试一次"); } else if (i < a) { System.out.println("你猜小了,再试试"); } else { System.out.printf("恭喜你猜对了,最后得分:%d%n", 100 - (b - 1) * 10); break; } } System.out.println("游戏结束!"); }
最后
以上就是听话墨镜最近收集整理的关于猜数游戏,随机生成一个1~100的数进行猜测。的全部内容,更多相关猜数游戏,随机生成一个1~100内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复