1 package cn.unnerd;
2
3 import java.util.InputMismatchException;
4 import java.util.Scanner;
5
6 public class Rand {
7
8 public static void main(String[] args) {
//try为异常处理.处理输入非数字的异常情况
9 try {
10 int guess = 0;
11 int count = 0;
12
13 // 取随机数方法一
14 // Random bb = new Random();
15 // int number1 = bb.nextInt(100) + 1;
16
17 // 取随机数方法二
18 int number = (int)(Math.random() * 1000);
19
20 System.out.println("请输入0-1000的整数");
21 Scanner aa = new Scanner(System.in);
22 guess = aa.nextInt();
23 while (guess <= 1000 && guess >= 0) {
24 if (guess < number) {
25 System.out.println("太小了,再输一次");
26 guess = aa.nextInt();
27 count++;
28
29 } else if (guess > number) {
30 System.out.println("太大了,再输一次");
31 guess = aa.nextInt();
32 count++;
33 } else if (guess == number) {
34 System.out.println("恭喜你猜对了");
35 count++;
36 break;
37 }
38 }
39 if (count == 1)
40 System.out.println("运气爆表,奖励金砖");
41 else if (count > 1 && count <= 7)
42 System.out.println("不错哦,奖励银砖");
43 else if (count > 7)
44 System.out.println("太菜了,奖励板砖");
45 else {
46 System.out.println("请根据提示输入正确的数字");
47 }
48 } catch (InputMismatchException e) {
49 System.out.println("请根据提示输入正确的数字");
//使用递归再次运行该进程
50 Rand.main(null);
51 }
52 }
53
54 }