为了账号安全,请及时绑定邮箱和手机立即绑定

为什么String book = a.next();无论打什么进去都不抛出异常

package com.bbook;
import java.util.InputMismatchException;
import java.util.Scanner; 

public class Bbook {     
            public static void main(String[] args) {           
            //下面注释有打问号的,都不确定            
            // 定义字符串数组         
            String[] books={"c语言","数据结构","数据库原理","操作系统原理","计算机网络基础"};          
            //使用while语句实现重新输入?          
            while(true){
                                        
            try {                                                             
                    System.out.println("输入命令:1-按名称查找图书;2-按序号查找图书");                                         
                    Scanner a = new Scanner(System.in);                                         
                    int i = a.nextInt();//缓存输入整型键值
                                                               
                    if(i==1){//当输入1时,通过输入名称寻找图书  
                                                                                                                                                   
                    System.out.println("请输入图书名称:");                                                 
                    String book = a.next();//缓存输入字符串键值,为什么这里输入什么值都没有抛出异常?求解!                                                 
                    String aimbook = null;  
                                                                                                                          
                    for(int j = 0; j<books.length; j++)//for语句进行匹配                                                  
                    {                                                           
                        if(book.equals(books[j]))                                                           
                        {                                                                   
                            aimbook=books[j];                                                                   
                            break;                                                          
                        }else{                                                                      
                                    throw new NameException("图书不存在");//自定义异常抛出                                                            
                                }                                                   
                     } 
                                                                       
                     System.out.println("book:"+aimbook); 
                                                                                                                                                                              
                     }else if(i==2){//当输入2时,通过输入序号寻找图书 
                                                                                                                                                     
                     System.out.println("请输入图书序号:");                                                 
                     int n= a.nextInt();//缓存输入整形键值                                                 
                     String aimbook = null; 
                                                                                                                            
                     for(int j = 0; j<books.length; j++)//for语句进行匹配                                                  
                     {                                                           
                         if(n>books.length+1)                                                         
                         {                                                                   
                             throw new NameException("图书不存在");//自定义异常抛出                                                            
                          }else if(n-1==j)                                                           
                          {                                                                   
                              aimbook=books[j];                                                                   
                              break;                                                          
                          };                                                  
                      } 
                                                                        
                      System.out.println("book:"+aimbook); 
                                                                                                                                                  
                      }else{//当输入1和2以外的整型数值时  
                                                                      
                      System.out.println("命令不存在!请输入1或者2");  
                                                                
                      }  
                      
                                                                        
                      } catch (InputMismatchException e) {                           
                           // 捕获数字输入不匹配异常?                            
                           e.printStackTrace();                            
                           System.out.println("命令输入错误!请根据提示输入数字命令!");                  
                      }catch (NameException e) {                           
                          // 捕获自定义异常                         
                          e.printStackTrace();                            
                          System.out.println("图书不存在!");                   
                      }catch (Exception e) {                           
                          // TODO: handle exception                            
                          e.printStackTrace();                            
                          System.out.println("未知异常!");                        
                      }               
                  }       
              }
         } 
                      
                          
 package com.bbook;
 
 public class NameException extends Exception{ 
       
     public NameException(){            
    } 
           
     public NameException(String massage){             
         super(massage);     
         }
    }



正在回答

2 回答

因为你输入什么都当做字符串类型了,比如1,2,3,红楼梦...

0 回复 有任何疑惑可以回复我~

scanner 是扫描输入文本类,当你创建a对象是并不是说你创建了一个int类型或Sting 类型,你想输入什么类型只需要调用什么方法即可

比如、

String 可以用next方法  或者是nextLine方法

int可以用nextInt

而scanner对象可以是一个

求采纳

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

为什么String book = a.next();无论打什么进去都不抛出异常

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信