交作业了,萌新求指教。
package mytest;
import java.util.Scanner;
public class Hdl {
static String s[]=new String[]{"高数","大学英语","政治","java"};
public static void main(String[] args) {
// TODO 自动生成的方法存根
Choose();
}
public static void Choose(){
System.out.println("输入命令:1—按照名称查找图书;2-按照序号查找图书");
try{
Scanner sc=new Scanner(System.in);
int i=sc.nextInt();
if(i==1){
System.out.println("输入图书名称:");
tushu();
}else if(i==2){
System.out.println("请输入序号:");
xuhao();
}else {
System.out.println("输入的命令有误,请根据提示输入数字!");
Choose();
}
}catch(Exception e){
}
}
public static void tushu(){
try{
Scanner a=new Scanner(System.in);
String x=a.nextLine();
Name(x);
if(x.equals(s[0])){
System.out.println("book:"+s[0]);}
else if(x.equals(s[1])){
System.out.println("book:"+s[1]);}
else if(x.equals(s[2])){
System.out.println("book:"+s[2]);}
else if(x.equals(s[3])){
System.out.println("book:"+s[3]);
}
}catch(Exception e){
System.out.println(e.getMessage());
Choose();
}
}
public static void xuhao(){
try{
Scanner b=new Scanner(System.in);
int y=b.nextInt();
Number(y);
if(y==1){
System.out.println("book:"+s[0]);}
else if(y==2){
System.out.println("book:"+s[1]);}
else if(y==3){
System.out.println("book:"+s[2]);}
else if(y==4){
System.out.println("book:"+s[3]);
}
}catch(Exception e){
System.out.println(e.getMessage());
Choose();
}
}
public static String Name(String x)throws Exception{
if(!(x.equals(s[0]))&&!(x.equals(s[1]))&&!(x.equals(s[2]))&&!(x.equals(s[3]))){
throw new Exception("图书不存在");
}
return x;
}
public static int Number(int y)throws Exception{
if(y<1^y>4){
throw new Exception("图书不存在");
}
return y;
}
}