到 错误命令异常就不能正常循环这是为什么

import java.util.*;

public class CheckBook{

  public static final String[] book = {"春天日记","高等数学","计算机概述","网络设计","论语","青春的躁动","大学英语"};

  public static boolean right = true;

  Scanner scanner = new Scanner(System.in);

  public static void main(String[] args){

    CheckBook checkBook = new CheckBook();

    while(right){

      try{

        checkBook.test1();

      }catch(Exce e){

        e.printStackTrace();

      }

   }

  }

  public void test1()throws Exce {

      System.out.println("请输入您要查询的id:");

      if(scanner.hasNextInt()){

        int number = scanner.nextInt();

        if(number>book.length || number<0){

          throw new Exce("序号不存在!");

        }else{

          System.out.println("请输入书籍名称:");         

          String bookName = scanner.next();

          if(bookName.equals(book[number-1])){

            System.out.println("图书馆中有此书,书的序号为:"+number);

            right = false;

          }else{

            throw new Exce("图书不存在!");

          }

        }

      }else{

        throw new Exce ("错误命令异常!");

      }

    }

}


weibo_快乐is魏帆_0
浏览 1281回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java