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

作业代码,输入视频中的参数和自己定义的参数都可以成功,大家看看哪里还有不足的地方

//新建一个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();

}

}

}


输出结果图:

https://img2.mukewang.com/5c446e400001243104640357.jpg

https://img2.mukewang.com/5c446e620001d64204980365.jpg

https://img3.mukewang.com/5c446eb10001b39b04060301.jpg

https://img2.mukewang.com/5c446eda0001106b05030313.jpg

提问者:Daylight_黎明6 2019-01-20 20:51

个回答