package BBB;
import java.util.Scanner;
public class BookSystem {
int choice;
public int getChoice() {
return choice;
}
public void setChoice(int choice) {
this.choice = choice;
}
public String[] book={"沉默的大多数","红拂夜奔","黄金时代","绿毛水怪","三十而立","猫","万寿寺"};
public String[] number={"1","2","3","4","5","6","7"};
public void hello(){
System.out.println("输入命令:1-按照序号查找图书;2-按照名称查找图书");
do
try{
Scanner input1=new Scanner(System.in);
choice=input1.nextInt();
}catch(Exception e){
System.out.println("请输入1或2!");
}while(choice!=1&&choice!=2);
}
public void method1()throws NoExistException{
boolean bool=true;
do
try{System.out.println("请输入图书序号");
Scanner input2=new Scanner(System.in);
double num=input2.nextDouble();
if(num<number.length)
{System.out.println("你要找的书序号是:"+number[(int) num-1]+",书名是:"+book[(int) num-1]);
bool=false;
}
if(num>number.length)
{ bool=true;}
if(bool==true)
{throw new NoExistException();}
}catch(NoExistException e){
e.show();
}while(bool==true);
}
public void method2()throws NoExistException{
System.out.println("请输入图书书名:");
boolean bool = true;
do
try{
Scanner input2=new Scanner(System.in);
String name=input2.next();
for(int i=0;i<book.length;i++) {
if(book[i].equals(name)){
System.out.println("你要找的书序号是:"+number[i]+",书名是:"+book[i]);
bool = false;
}}
if(bool==true)
throw new NoExistException();
}catch(NoExistException e){
e.show();
}while(bool==true);
}
}
package BBB;
@SuppressWarnings("serial")
public class NoExistException extends Exception {
public void show(){
System.out.println("该书不存在,请重新输入!");
}
}
public class Test {
/**
* @param args
* @throws NoExistException
*/
public static void main(String[] args) throws NoExistException {
// TODO Auto-generated method stub
BookSystem user=new BookSystem();
user.hello();
switch(user.choice){
case 1:
user.method1();
break;
case 2:
user.method2();
break;}
System.out.println();
System.out.println();
System.out.println("谢谢使用。");
}
2025 - 快车库 - 我的知识库 重庆启连科技有限公司 渝ICP备16002641号-10
企客连连 表单助手 企服开发 榜单123