问答详情
源自:1-9 经验总结

为什么在输入图书名称之后不会运行下面的输出Book的语句呢

package com.BorrowBooks;


import java.util.Scanner;


public class book {
  public static void main(String[] args) {
   for(;;){
   System.out.println("欢迎使用借书系统");
   System.out.println("输入命令:1.按名称查找图书   2.按序号查找图书");
   Scanner in=new Scanner(System.in);
   int choice = 3;
   int i = 0;
   int j = 0;
   String[] Book = {"高数","线代","概率论与数理统计","大学英语","数据库","操作系统","java面向对象"};
   try{
    choice = in.nextInt();
   }catch(Exception e){
    System.out.println("命令输入错误,请根据提示输入整形的数字");
    continue;
   }
   if(choice == 1){
    System.out.println("请输入你要借的书的名称");
    try{
     for(i=0;i<Book.length;i++){
      if(Book[i]==in.nextLine()){
       System.out.println("Book:"+Book[i]);
       continue; 
     }
    }catch(Exception e){
     System.out.println("图书不存在");
     continue;
    }
   }else if(choice == 2){
    System.out.println("请输入你要借的书的序号");
    try{
     for(j=0;j<Book.length;j++){
      if(j==in.nextInt()){
       System.out.println("Book:"+Book[i]);
       continue;
                }
    }catch(Exception e){
     System.out.println("图书不存在");
     continue;
    }
   }else {
    System.out.println("你的选择错误");
   }
  }
  }
 }


提问者:Destiny命运 2016-08-01 23:19

个回答

  • 慕瓜7080925
    2016-08-05 20:15:45

    Exception无法转换为Throwable

  • 慕瓜7080925
    2016-08-05 20:12:24

    你这写的 问题太多了

  • Destiny命运
    2016-08-02 10:42:44

    是我傻了哦,这样写就好了

    package com.BorrowBooks;


    //import java.awt.print.Book;
    import java.util.Scanner;


    public class book {
      @SuppressWarnings("resource")
      public static void main(String[] args){
       for(;;){
       System.out.println("欢迎使用借书系统");
       System.out.println("输入命令:1.按名称查找图书   2.按序号查找图书");
       Scanner in=new Scanner(System.in);
       int choice = 3;
    //   int i= 0;
    //   int j = 0;
    //   String[] Book = {"高数","线代","概率论与数理统计","大学英语","数据库","操作系统","java面向对象"};
       try{
        choice = in.nextInt();
       }catch(Exception e){
        System.out.println("命令输入错误,请根据提示输入整形的数字");
        continue;
       }
       if(choice == 1){
    //    System.out.println("请输入你要借的书的名称");
    //    for(i=0;i<Book.length;i++){
    //    try{
    //        in.nextLine().equals(Book[i]);
    //       System.out.println("Book:"+Book[i]);
    //       break;
    //    }catch(Exception e){
    //     System.out.println("图书不存在");
    //     continue;
    //    }
    //    }
    //   System.out.println("Book:"+Book[i]);
        book Mc = new book();
        Mc.find1();
       }else if(choice == 2){
    //    System.out.println("请输入你要借的书的序号");
    //    for(j=0;j<Book.length;j++){
    //    try{
    //       j=in.nextInt();
    //     System.out.println("Book:"+Book[j]);
    //     break;
    //    }catch(Exception e){
    //     System.out.println("图书不存在");
    //     continue;
    //    }
    //    }
    //    System.out.println("Book:"+Book[j]);
        book Xh = new book();
        Xh.find2();
       }else {
        System.out.println("你的选择错误");
       }
      }
      }
      @SuppressWarnings("resource")
      public void find1(){
       int i = 0;
       String[] Book = {"高数","线代","概率论与数理统计","大学英语","数据库","操作系统","java面向对象"};
       System.out.println("请输入你要借的书的名称");
       Scanner in=new Scanner(System.in);
       String H = in.next();
       for(i=0;i<Book.length;i++){
       try{
           H.equals(Book[i]);
          System.out.println("Book:"+H);
          break;
       }catch(Exception e){
        System.out.println("图书不存在");
        continue;
       }
       }
      }
      @SuppressWarnings("resource")
      public void find2(){
       int j = 0;
       String[] Book = {"高数","线代","概率论与数理统计","大学英语","数据库","操作系统","java面向对象"};
       System.out.println("请输入你要借的书的序号");
       Scanner in=new Scanner(System.in);
       for(j=0;j<Book.length;j++){
       try{
          j=in.nextInt();
        System.out.println("Book:"+Book[j]);
        break;
       }catch(Exception e){
        System.out.println("图书不存在");
        continue;
       }
       }
      }
     }


  • JJJava
    2016-08-01 23:51:09

    根据你的代码你必须输入Book.length次才执行输出语句,而且你的判断语句后面没有执行语句?if完然后干嘛?