package cn;
import java.util.Random;
import java.util.Scanner;
/**
* @author yeqv
* @program A2
* @Classname a1
* @Date 2022/1/14 21:17
* @Email w16638771062@163.com
*/
public class a1 {
//猜数字小程序,利用异常处理
public static void main(String[] args) {
Random random = new Random();
Scanner scanner = new Scanner(System.in);
int a = random.nextInt(1, 101);
int b = 0;
while (true) {
try {
System.out.println("请输入一个1-100之间的数字:");
b = scanner.nextInt();
if (b > a) {
System.out.println("猜大了");
} else if (b < a) {
System.out.println("猜小了");
} else {
System.out.println("猜对了");
break;
}
} catch (Exception e) {//这里捕获一个异常,如果输入的不是数字将被捕获
System.out.println("输入错误,请输入1-100之间的数字");
scanner.next();
}
}
}
}