发一下我的答案

来源:1-9 经验总结

退回无路AI

2020-03-16 17:50

public class ChaShu {

public static void main(String[] args) {

ChaShu cs = new ChaShu();

cs.FindBook();

}

public void FindBook() {

while(true) {

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

Scanner sc = new Scanner(System.in);

try {

int i =sc.nextInt();

if(i==1) {

String bookname = null;

First(bookname );

}

if(i==2) {

int a = 0;

Second(a);

}else{

System.out.println("输入错误,请重新输入");

}

}catch(Exception e) {

System.out.println(e.getMessage());

}

}

}

public void First(String bookname) {

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

String book[] = {"高数","英语","语文"};

Scanner sc = new Scanner(System.in);

bookname = sc.nextLine();

try {

int a=1000000;

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

if(bookname.equals(book[i])) {

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

a=-1;

}

}

if(a==1000000) {

throw new Exception();

}

}catch(Exception e) {

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

}

}

public void Second(int a) {

System.out.println("请输入图书id");

Scanner sc = new Scanner(System.in);

String book[] = {"高数","英语","语文"};

a = sc.nextInt();

if(a>0 && a<=book.length) {

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

}else {

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

}

}

}


写回答 关注

1回答

  • 慕莱坞0206130
    2020-03-23 11:28:45

    牛批!

Java入门第三季

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

409792 学习 · 4340 问题

查看课程

相似问题