- import java.util.InputMismatchException;
- import java.util.Scanner;
- public class RandomTest {
- public static void main(String[] args) {
- int number = (int) (Math.random() * 100) + 1;
- int count = 0;
- while (true) {
- try {
- Scanner sc = new Scanner(System.in);
- System.out.println("请输入1-100之间的数据:");
- int guessNumber = sc.nextInt();
- count++;
- if (guessNumber > number) {
- System.out.println("你猜的数据" + guessNumber + "大了");
- } else if (guessNumber < number) {
- System.out.println("你猜的数据" + guessNumber + "小了");
- } else {
- System.out.println("恭喜你,"+count+"次就猜对了");
- break;
- }
- } catch (InputMismatchException ie) {
- System.out.println("你输入的数据有误");
- }
- }
- }
- }
复制代码 |
|