复制代码1
2
3
4
5
6/* * 1. 输入0,直接退出游戏 * 2. 记录成绩 1-3 A 4-6 B 7-10 C * 3. 超过10次就直接退出游戏,永久封号 * 4. 输入游戏难度 1 1-50 2 1-100 3 1-200 */
复制代码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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82package com.qf; import java.util.Scanner; public class Demo03{ public static void main(String[] args) { //定义扫描器 Scanner scan = new Scanner(System.in); //提示用户输入游戏等级 System.out.println("请输入游戏等级 1容易 2普通 3困难 :"); int lever = scan.nextInt(); //自动生成[1,100]的数字 int num; String scope; if(lever == 1) { num = (int) (Math.random()*50+1); //[1,50] scope = "[1-50]"; } else if(lever == 2) { num = (int) (Math.random()*100+1); //[1,100] scope = "[1-100]"; } else { num = (int) (Math.random()*200+1); //[1,200] scope = "[1-200]"; } //提示用户输入 System.out.println("请输入您猜的数字"+scope+":"); //扫描器扫描得到数字 int guessNum = scan.nextInt(); int count = 1; //定义变量,记录猜的次数 //没有猜对的时候,继续猜 while(num != guessNum) { if(guessNum == 0) { // 主动退出 break; } if (guessNum > num) { System.out.println("大了,请输入您猜的数字"+scope+":"); } else { System.out.println("小了,请输入您猜的数字"+scope+":"); } //使用guessNum接收用户再次输入的值,进行判断 guessNum = scan.nextInt(); if(++count >10) { //超过10次,主动退出 break; } } if(guessNum == 0) { System.out.println("退出"); } else { if(count<=3) { System.out.println("A你猜对了"); } else if(count <=6) { System.out.println("B你猜对了"); } else if(count <=10) { System.out.println("C你猜对了"); } else { System.out.println("永久封号!!"); } } } }
最后
以上就是苹果小蚂蚁最近收集整理的关于java基础——使用循环设计猜数字小游戏的全部内容,更多相关java基础——使用循环设计猜数字小游戏内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复