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

打卡-交作业

package Demo;


import java.util.Scanner;


public class JieShu {

public static void main(String[] args) {

//System.out.println("*******欢迎来到慕课网借书系统*******");

JieShu test=new JieShu();

test.BorrowBook();

}

public  void BorrowBook() {

System.out.println("输入命令:1-按照名称查找图书;2-按照序号查找图书");

Scanner input=new Scanner(System.in);

try {

int num=input.nextInt();

if(num==1) {

MingCheng();

}else if(num==2) {

XuHao();

}

}catch(Exception e) {

System.out.println("命令输入错误!请根据提示输入数字命令");

BorrowBook();

}

}

public void MingCheng(){

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

Scanner input1=new Scanner(System.in);

try{

String Bookname =input1.nextLine();

MingChengBook(Bookname);

}catch(Exception e){

System.out.println("命令输入错误!请根据提示输入数字命令");

BorrowBook();

}

}

public void XuHao(){

System.out.println("输入图书名序号:");

Scanner input2=new Scanner(System.in);

try{

int Booknum =input2.nextInt();

XuHaoBook(Booknum);

}catch(Exception e){

System.out.println("命令输入错误!请根据提示输入数字命令");

XuHao();

}

}

public void MingChengBook(String Bookname) {

String Books[]= {"数据结构","高数","java语言","英语"};

try {

int a=-1;

for(int i=0;i<Books.length;i++) {

if(Books[i].equals(Bookname)) {

System.out.println("book:"+Books[i]);

a=1;

}

}

if(a==-1) {

throw new Exception();

}

}catch(Exception e){

System.out.println("图书不存在!");

BorrowBook();

}  

}

public void XuHaoBook(int Booknum) {

String Books[]= {"数据结构","高数","java语言","英语"};

try {

if(Booknum>0&&Booknum<=Books.length){

System.out.println("book:"+Books[Booknum-1]);

}else {

throw new Exception();

}

}catch(Exception e){

System.out.println("图书不存在!");

BorrowBook();

}

}

}


提问者:慕丝4409378 2019-04-14 16:05

个回答

  • 我想要两颗西柚123
    2019-04-16 19:04:08
    已采纳

    好像一开始不输入1或者2 的话也不会抛出异常呀~~~