交作業(yè)了,萌新求指教。
package mytest;
import java.util.Scanner;
public class Hdl {
static String s[]=new String[]{"高數(shù)","大學英語","政治","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("輸入的命令有誤,請根據(jù)提示輸入數(shù)字!");
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;
}
}
2020-07-25
牛掰啊