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

求个大神指出错误...

import java.util.InputMismatchException;
import java.util.Scanner;
public class Book {

 public String books[]={"物理","化学","历史","政治"};
 public static void main(String[] args) {
  Book Use=new Book();
  Use.test();
  
  
  // TODO Auto-generated method stub

 }
public  void test(){
Scanner input=new Scanner(System.in);
System.out.println("输入命令:1-按照名称查找图书;2-按照序号查找图书");
try{
 int a=input.nextInt();
 switch(a){
 case 1:
  System.out.println("请输入图书名称");
  Scanner as=new Scanner(System.in);
  String name=as.next();
    for(int i=0;i<books.length;i++){
         if(books[i].equals(name)){ 
        System.out.println("book:"+books[i]);
         test();
      }
    }
       System.out.println("图书不存在,请重新输入");
    test();
 case 2:
  System.out.println("请输入图书序号");
  try{
  int d=input.nextInt();
  for(int i=0;i<books.length;i++){
   if(d==i+1){
   System.out.println("book"+books[i]); 
   }
  }
  test();
  }catch(InputMismatchException d ){
   System.out.println("输入有误,请输入正确的图书序号");
   test();
  }
 
 }
 
}catch(Exception e){
 System.out.println("命令输入错误!请根据提示输入数字命令");
 test();
}
 
}
 
}

http://img1.sycdn.imooc.com//55c317660001f1f514400874.jpg

正在回答

2 回答

楼主的问题是啥?在我这运行的没有报错啊?

0 回复 有任何疑惑可以回复我~
package com.pun;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Book {



	public static void main(String[] args) {
		Book Use=new Book();
		Use.test();


		// TODO Auto-generated method stub
	}
	public  void test(){
		String[] books={"物理","化学","历史","政治"};
		Scanner input=new Scanner(System.in);
		System.out.println("输入命令:1-按照名称查找图书;2-按照序号查找图书"); 
		try{
			int a=input.nextInt();
			switch(a){
			case 1:
				System.out.println("请输入图书名称");
				Scanner as=new Scanner(System.in);
				String name=as.next();
				for(int i=0;i<books.length;i++){
					if(books[i].equals(name)){  
						System.out.println("book:"+books[i]); 
						test();
					}
				}
				System.out.println("图书不存在,请重新输入"); 
				test();
			case 2:
				System.out.println("请输入图书序号");
				try{
					int d=input.nextInt();
					for(int i=0;i<books.length;i++){
						if(d==i+1){
							System.out.println("book"+books[i]); 
						}
					}
					test();
				}catch(InputMismatchException d ){
					System.out.println("输入有误,请输入正确的图书序号");
					test();
				}

			}

		}catch(Exception e){
			System.out.println("命令输入错误!请根据提示输入数字命令");
			test();
		}

	}

}


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

举报

0/150
提交
取消
Java入门第三季
  • 参与学习       409775    人
  • 解答问题       4546    个

Java中你必须懂得常用技能,不容错过的精彩,快来加入吧

进入课程

求个大神指出错误...

我要回答 关注问题
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号