用random定义一个随机数,再进行猜测,给出提示。
复制代码
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
29package project; import java.util.Random; import java.util.Scanner; public class Java { public static void main(String[] args) { Random r = new Random(); int num = r.nextInt(100) + 1; Scanner sc = new Scanner(System.in); int count=0; while (true) { System.out.printf("请输入你要猜的数字"); int i = sc.nextInt(); count++; if(count==3){ System.out.println("猜对了");break; } if (i > num) { System.out.println("大啦"); } else if(i < num){ System.out.println("小啦"); }else{ System.out.println("正好"); } } } }
最后
以上就是虚幻心情最近收集整理的关于随机数猜数字的全部内容,更多相关随机数猜数字内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复