DDDwant
2019-08-17 20:01
package jieshuxitong; import java.util.Scanner; import java.util.InputMismatchException; public class Jieshu {public static void main(String[] args) { Jieshu js = new Jieshu(); js.runtime(); } public class NoBookException extends Exception{public NoBookException() { } public NoBookException(String message) { super(message); } } public class SrcwException extends Exception{ public SrcwException(String message) { super(message); } } public void runtime() { String[] books = {"人间失格","我是猫","月亮和六便士","格物致知"}; Scanner sc = new Scanner(System.in); System.out.println("******欢迎使用图书租赁系统******"); System.out.println("1、按照名称查找图书; 2、按照序号查找图书"); try { int sr = sc.nextInt(); if( sr == 1 ) { System.out.println("请输入书的名称:"); String name = sc.next(); int bl = 0; for(int i = 0;i<books.length;i++) { if(books[i].equals(name)) { bl= i; }else { continue; } } if(books[bl].equals(name)) { System.out.println("书来啦~~~"+books[bl]); }else { throw new NoBookException(); } }else if( sr == 2 ){ System.out.println("请输入书的序号:"); int num = sc.nextInt(); if(num>0 && num<=books.length) { System.out.println("书来啦~~~:"+books[num-1]); }else { throw new NoBookException("输入错误,重输:"); } }else {throw new SrcwException("输入错误,请重新输入:"); } }catch(NoBookException e){System.out.println("查无此书,重输:"); runtime(); }catch(SrcwException e) {System.out.println(e.getMessage()); runtime(); }catch(InputMismatchException e) {System.out.println("请按提示重新输入!"); runtime(); } } }
import java.util.Scanner; import java.util.InputMismatchException; public class Jieshu {
Java入门第三季
409792 学习 · 4340 问题
相似问题