手记

模拟借书系统规范代码

package project2;

public class Myexception extends Exception {
Myexception(String str){
super(str);
}
}

package project2;

import java.util.Scanner;

public class shoushu {
String tushu[]=new String[]{"高数","数据结构","计算机网络","专业英语"};
public void mingcheng(String a)throws Myexception{
String s="\d+";
boolean b=false;
int i;
if(a.matches(s) ){
throw new Myexception("指令错误!");
}else{
for(i=0;i<tushu.length;i++){
if(a.equals(tushu[i])) {
System.out.println("book: "+a);break;
}
}
if(i==tushu.length){
throw new Myexception("图书不存在!");
}
}
}
public void shuzi(int num) throws Myexception{
if(num>=tushu.length){
throw new Myexception("图书不存在");
}else{
System.out.println("book: "+tushu[num]);
}
}
public static void main(String[] args) {
// TODO 自动生成的方法存根
int i;
String shuming;
shoushu a=new shoushu();
while(true){
System.out.println("输入命令:1.按照名称找图书 2.按照序列号找图书");
Scanner sc=new Scanner(System.in);
i=sc.nextInt();
if(i==1){
System.out.println("输入图书名称:");
Scanner sc2=new Scanner(System.in);
shuming=sc2.nextLine();
try{
a.mingcheng(shuming);
break;
}catch(Myexception e){
System.out.println(e);
}
}else if(i==2){
System.out.println("输入图书序列号:");
try{
Scanner sc3=new Scanner(System.in);
i=sc3.nextInt();
a.shuzi(i);
break;
}catch(Myexception e){
System.out.println(e);
}
catch(Exception e){
System.out.println("命令错误");
}
}else {
System.out.println("系统即将退出!!");break;
}
}

}

}

0人推荐
随时随地看视频
慕课网APP