作业代码,根据上面的提示和自己的提示试了一下,可以运行的。大家可以看一下对不对
//新建一个BookSystem类
import java.util.*;
public class BookSystem {
/**
* @param args
* @throws BooknameException
*/
public static void main(String[] args) throws BooknameException {
// TODO Auto-generated method stub
BookSystem bstem=new BookSystem();
int num1=bstem.input();
if(num1==1){
bstem.booknamer();
}
if(num1==2){
bstem.booknumber();
}
}
public int input(){
try {
System.out.println("输入命令:1-按照名称查找图书; 2-按照序号查找图书");
Scanner s=new Scanner(System.in);
int inputnum=s.nextInt();
while(inputnum!=1 && inputnum!=2){
System.out.println("命令输入错误!请根据提示输入指定命令。");
System.out.println("输入命令:1-按照名称查找图书; 2-按照序号查找图书");
inputnum=s.nextInt();
}
return inputnum;
} catch (Exception e) {
// TODO: handle exception
System.out.println("命令输入错误!请根据提示输入数字命令。");
return input();
}
}
public void booknamer() throws BooknameException{
Bookname bookname=new Bookname();
Scanner s=new Scanner(System.in);
try {
System.out.println("输入图书名称");
bookname.bijiao(s.next());
} catch (BooknameException e) {
// TODO: handle exception
input();
System.out.println("输入图书名称");
bookname.bijiao(s.next());
}
}
public void booknumber()throws BooknameException{
Booknumber booknumber=new Booknumber();
Scanner s=new Scanner(System.in);
try {
System.out.println("输入图书序号");
booknumber.bijiao(s.nextInt());
} catch (BooknameException e) {
// TODO: handle exception
input();
System.out.println("输入图书序号");
booknumber.bijiao(s.nextInt());
}catch (InputMismatchException e) {
// TODO: handle exception
System.out.println("命令输入错误!请根据提示输入数字命令。");
booknumber();
}
}
}
//新建一个BooknameException类
public class BooknameException extends Exception{
public BooknameException(){
System.out.println("图书不存在!");
}
}
//新建一个Bookname 类
public class Bookname {
public void bijiao(String name)throws BooknameException{
switch (name) {
case "中华字典":System.out.println("book:中华字典");break;
case "高数":System.out.println("book:高数");break;
case "文学经典":System.out.println("book:文学经典");break;
case "趣味物理":System.out.println("book:趣味物理");break;
case "综合化学":System.out.println("book:综合化学");break;
default:throw new BooknameException();
}
}
}
//新建一个Booknumber 类
public class Booknumber {
public void bijiao(int num)throws BooknameException{
switch (num) {
case 1:System.out.println("book:JAVA应用开发");break;
case 2:System.out.println("book:Android应用开发");break;
case 3:System.out.println("book:C#应用开发");break;
case 4:System.out.println("book:单片机原理");break;
case 5:System.out.println("book:计算机组成原理");break;
default:throw new BooknameException();
}
}
}