錯在哪兒了
package Imooc_Exception;
import java.util.Scanner;
/*
模擬借書系統
要求
1、定義字符串數組保存圖書信息
2、提示用戶輸入,分別按“書名”和“圖書序號”查找圖書
3、根據輸入信息進行適當的異常處理
a、如果輸入類型錯誤,拋出“錯誤命令異?!辈⑻崾局匦螺斎?/p>
b、如果書名不存在,拋出“圖書不存在異常”并提示重新輸入
c、如果圖書序號超過字符串數組范圍,拋出“圖書不存在異常
并提示重新輸入
*/
public class BorrowBook_Initial {
public static void main(String[] args){
String[] Books={"1:論語","2:大學","3:中庸","4:孟子","5:數據結構","6:C++"};
for (String book:Books){
System.out.println(Books);
}
System.out.println("本系統提供按書名和圖書序號查找圖書"+"\n"+"1-代表按書名查詢圖書"+"\t"+"2-代表按序號查詢圖書");
Scanner inputa= new Scanner(System.in);
int a=inputa.nextInt();
public void SearchBook() throws Exception{//為什么這里一直報錯!
if (a!=1 && a!=2){
throw new Exception ("您輸入的不正確,請重新輸入!");}
else?
if (a==1) {System.out.println("請輸入要查詢的圖書的書名!");
Scanner inputb= new Scanner(System.in);
String b=inputb.next();
for (int i=0;i<Books.length;i++)
{
if (b==Books[i])
{
System.out.println(Books[i]);
}
else throw new Exception("圖書不存在!");
}
?
}
else
{System.out.println("請輸入要查詢的圖書的序號!");
Scanner inputc= new Scanner(System.in);
//int c=inputc.nextInt();
String c=inputc.next();
for (int i=0;i<Books.length;i++)
{
if (c==Books[i])
{
System.out.println(Books[i]);
}
else throw new Exception("圖書不存在!");
}
}?
}
?
?
}
}
2019-09-11
你在方法里面直接又寫了個方法,這是不合法的
2019-09-04
你的void SearchBook() 方法定義在了main方法里面,這樣是不允許的.
2019-08-21
有空的蝦們多幫忙看看呀