亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

交作業了,萌新求指教。

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;

}

}


正在回答

1 回答

我碰巧看到了,那我就指出一下美中不足的地方吧,首先源碼缺乏對于輸入輸出流的關閉,其次呢if?else過多,如果能夠將大量的if?else都用異常來處理就好了。然后呢,關于字符串匹配好像可以用現成的庫方法調用

0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

交作業了,萌新求指教。

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號