求教啊。。不管怎樣,都是異常報(bào)出。不懂
package exception_test;
import java.util.*;//導(dǎo)入java工具
public class Test_Exception {
/*圖書查找程序:
?* 1.提示用戶輸入,分別按書名及圖書序號(hào)來查找圖書
?* 2.根據(jù)輸入信息進(jìn)行適當(dāng)?shù)漠惓L幚?br />?*/
?? ?public static void main(String[] args) {
?? ??? ?// TODO Auto-generated method stub
?? ??? ?show();
?? ??? ?
?? ??? ?
?? ?}
?? ?public static void books(String bookname){
?? ??? ?String[] book={"論語","高數(shù)","日語","英語","漢語"};
?? ??? ?
?? ??? ?try{
?? ??? ??? ?for(int i =0;i<book.length;i++){
?? ??? ??? ?if(book[i].equals(bookname)){
?? ??? ??? ??? ?System.out.println("book:"+book[i]);
?? ??? ??? ?}else {
?? ??? ??? ??? ?throw new Exception();
?? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?}catch (Exception e){
?? ??? ??? ?e.printStackTrace();
?? ??? ??? ?System.out.println("您查詢的書名不存在: "+"\n" + "如有需要請(qǐng)重新選擇:");?? ?
?? ??? ??? ?show();
?? ??? ?}
?? ?}
?? ?public static void bookNum(int BookNum){
?? ??? ?String[] book={"論語","高數(shù)","日語","英語","漢語"};
?? ??? ?try{
?? ??? ??? ?int bookNum =0;
?? ??? ??? ?if(bookNum >0 && bookNum<book.length){
?? ??? ??? ??? ?System.out.println("book:"+book[bookNum-1]);
?? ??? ??? ?}else{
?? ??? ??? ??? ?throw new Exception();
?? ??? ??? ?}
?? ??? ?}catch(Exception e){
?? ??? ??? ?e.printStackTrace();
?? ??? ??? ?System.out.println("您查詢的書籍不存在: "+"\n" + "如有需要請(qǐng)重新選擇:");?? ?
?? ??? ??? ?show();
?? ??? ?}
?? ?}
?? ?public static void bookname(String name){
?? ??? ?Scanner input2 = new Scanner(System.in);
?? ??? ?try{
?? ??? ??? ?System.out.println("請(qǐng)輸入您所需的書名:");
?? ??? ??? ?String name1 =input2.next();
?? ??? ??? ?books(name1);
?? ??? ?}catch(Exception e){
?? ??? ??? ?e.printStackTrace();
?? ??? ??? ?System.out.println("請(qǐng)重新輸入書名:");
?? ??? ??? ?return;
?? ??? ?}
?? ?}
?? ??? ?
?? ?public static void booknum(int num){
?? ??? ?Scanner input3 = new Scanner(System.in);
?? ??? ?try{
?? ??? ??? ?System.out.println("請(qǐng)輸入您所需的書號(hào):");
?? ??? ??? ?int n =input3.nextInt();
?? ??? ??? ?bookNum(n);
?? ??? ?}catch(Exception e){
?? ??? ??? ?e.printStackTrace();
?? ??? ??? ?System.out.println("請(qǐng)重新輸入書號(hào):");
?? ??? ??? ?return;
?? ??? ?}
?? ?}
?? ?
?? ?public static void show(){
?? ??? ?System.out.println("歡迎您來到閱讀快樂吧:書名查詢請(qǐng)選1;序號(hào)查詢請(qǐng)選2");
?? ??? ?Scanner input =new Scanner(System.in);
?? ??? ?int Num =input.nextInt();
?? ??? ?try{
?? ??? ??? if(Num ==1){
?? ??? ??? ?bookname(null);
?? ??? ?? }else if (Num==2){
?? ??? ??? ?booknum(Num);
?? ??? ?? }else{
?? ??? ??? ?? throw new Exception();
?? ??? ?? }
?? ??? }catch(Exception e){
?? ??? ? System.out.println("您輸入的信息有誤,請(qǐng)重新輸入:");
?? ??? ? show();
?? ????? }
???? }
}
2019-05-06
books(String bookname) 方法中
for(int i =0;i<book.length;i++){
?? ??? ??? ?if(book[i].equals(bookname)){
?? ??? ??? ??? ?System.out.println("book:"+book[i]);
?? ??? ??? ?}else {
?? ??? ??? ??? ?throw new Exception();
?? ??? ??? ?}
?? ??? ??? ?}
你這里遍歷了整個(gè)數(shù)組,所以即便你輸入了有的課程,循環(huán)依然會(huì)執(zhí)行,那么必然會(huì)出現(xiàn)不相等的情況,從而執(zhí)行??throw new Exception();
bookNum(int BookNum) 方法中
int bookNum =0;
if(bookNum >0 && bookNum<book.length) 沒有用形參BookNum,所以你輸入什么都會(huì)報(bào)錯(cuò)
另外,你這異常使用的方式很奇葩!程序有很大的可優(yōu)化空間。
2019-05-07
?public static void bookNum(int BookNum){
?? ??? ?String[] book={"論語","高數(shù)","日語","英語","漢語"};
?? ??? ?try{
?? ??? ??? ?int bookNum =0;
?? ??? ??? ?if(bookNum >0 && bookNum<book.length){
?? ??? ??? ??? ?System.out.println("book:"+book[bookNum-1]);
?? ??? ??? ?}else{
?? ??? ??? ??? ?throw new Exception();
?? ??? ??? ?}
?? ??? ?}catch(Exception e){
?? ??? ??? ?e.printStackTrace();
?? ??? ??? ?System.out.println("您查詢的書籍不存在: "+"\n" + "如有需要請(qǐng)重新選擇:");?? ?
?? ??? ??? ?show();
?? ??? ?}
?? ?}
System.out.println("book:"+book[bookNum-1]);這一行,你的bookNum=0,bookNum-1不就是-1了嗎?肯定報(bào)數(shù)組越界異常啊,另外你參數(shù)int BookNum也沒用上啊