zoukankan      html  css  js  c++  java
  • Java实现1到100之间的猜数字游戏(及异常处理)

    public class Homework21 {
     public static void main(String[] args) {
      Random rand = new Random();
      Scanner sc = new Scanner(System.in);
      int num = rand.nextInt(100) + 1;
      int t = 0;
      while (true) {
       System.out.println("请输入1-100之间的整数:");
       try {
        t = sc.nextInt();
       } catch (InputMismatchException e) {
        System.out.println("输入有误!请重新输入");
        sc.next();
        continue;
       }
       if (t < num) {
        System.out.println("太小了!");
       } else if (t > num) {
        System.out.println("太大了!");
       } else {
        System.out.println("恭喜你,答对了!");
        break;
       }
      }
     }
    }
  • 相关阅读:
    1015
    1016
    1014
    1002
    1010
    1006
    动态规划1001
    动态规划1002
    使用EF框架调用带有输出参数(output)的存储过程
    工程地质相关知识
  • 原文地址:https://www.cnblogs.com/liuyunche/p/13740003.html
Copyright © 2011-2022 走看看