//主函数
package test;
import java.util.Scanner;
public class main {
private static int order;
private static Scanner input;
public static void main (String[] args) {
input = new Scanner(System.in);
String[] books = { "数据结构", "高数", "大学英语", "离散数学" };
boolean flag = true;
while (flag) {
System.out.println(" 输入命令:1-按照名称查找图书 ; 2-按照序号查找图书");
order = input.nextInt();
try {
if (order == 1) {
System.out.println("输入图书名称:");
String name = input.next();
flag=bookSearch.nameSearch(books, name);
}
else if (order == 2) {
System.out.println("输入图书序号:");
int id = input.nextInt();
bookSearch.idSearch(books, id);
}
else {
System.out.println("命令错误输入!请根据提示输入数字命令!");
}
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
}
//功能实现
package test;
public class bookSearch extends Exception {
public class java {
}
public static boolean idSearch(String[] books, int id) throws Exception {
boolean flag = true;
if (id > books.length || id < 0)
throw new Exception("图书不存在");
else {
System.out.println("book:" + books[id - 1]);
flag = false;
}
return flag;
}
public static boolean nameSearch(String[] books, String name) throws Exception {
boolean flag = true;
for (int i = 0; i < books.length; i++) {
if (name.equals(books[i])) {
System.out.println("book:" + name);
flag = false;
break;
}
}
if (flag)
throw new Exception("图书不存在");
return flag;
}
}
点击查看更多内容
2人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦