import?java.util.Scanner;
import?java.util.Arrays;
public?class?Tushuguan?{
public?static?void?main(String[]?args)?{
//?TODO?Auto-generated?method?stub
Scanner?input=new?Scanner(System.in);
System.out.println("歡迎使用圖書管理系統(tǒng)!");
String?[]names={"語文","數(shù)學(xué)","英語","物理","化學(xué)"};//圖書名稱數(shù)組
int?f=1;
do?{
System.out.println("輸入命令:");
System.out.println("按照名稱查詢圖書請輸入1;");
System.out.println("按照序號查詢圖書請輸入2");
int?num=input.nextInt();//輸入命令序號
try{
if(num!=1&&num!=2){
throw?new?Exception();
}
}catch(Exception?e){
System.out.println("請輸入正確的命令!");
}
if(num==1){
System.out.println("請輸入圖書名稱:");
String?name=input.next();
try{
for(int?i=0;i<names.length;i++){
if(name.equals(names[i])){
System.out.println(names[i]);
}else{
throw?new?Exception();
}}
}catch(Exception?e){
????
System.out.println("您查找的圖書不存在!");
continue;
}
}
try{
if(num==2){
System.out.println("請輸入圖書序號:");
Arrays.sort(names);
int?nameNum=input.nextInt();
if(nameNum<5){
System.out.println(names[nameNum]);
}else{
throw?new?Exception();
}
}
}catch(Exception?e){
????
System.out.println("您查找的圖書不存在!");
continue;
}
}while(f==1);
input.close();
}
}
2016-10-29
你把那個else 改成這樣else if(i==names.length)
2016-10-29
package com.imooc;
import java.util.Scanner;
import java.util.Arrays;
public class Test {
??
? ? public static void main(String[] args) {
? ? ? ? // TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
System.out.println("歡迎使用圖書管理系統(tǒng)!");
String []names={"語文","數(shù)學(xué)","英語","物理","化學(xué)"};//圖書名稱數(shù)組
int f=1;
do {
? ? System.out.println("輸入命令:");
? ? System.out.println("按照名稱查詢圖書請輸入1;");
? ? System.out.println("按照序號查詢圖書請輸入2");
? ? int num=input.nextInt();//輸入命令序號
? ? try{
? ? ? ? if(num!=1&&num!=2){
? ? ? ? ? ? throw new Exception();
? ? ? ? } ?
? ? ? ? }catch(Exception e){
? ? ? ? System.out.println("請輸入正確的命令!"); ??
? ? ? ? }
? ? ??
? ? ? ? ? ? if(num==1){
? ? ? ? ? ? ? ? System.out.println("請輸入圖書名稱:");
? ? ? ? ? ? ? ? String name=input.next();
? ? ? ? ? ? ? ? try{
? ? ? ? ? ? ? ? for(int i=0;i<names.length;i++){
? ? ? ? ? ? ? ? ? ? if(name.equals(names[i])){
? ? ? ? ? ? ? ? ? ? ? ? System.out.println(names[i]);
? ? ? ? ? ? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? ? ? }else if(i==names.length){?
? ? ? ? ? ? ? ? ? ? throw new Exception();
? ? ? ? ? ? ? ? ? ? }}
? ? ? ? ? ? ? ? ? ? }catch(Exception e){
? ? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? System.out.println("您查找的圖書不存在!");
? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ??
? ? ? ? try{ ??
? ? ? ? ? ? if(num==2){
? ? ? ? ? ? System.out.println("請輸入圖書序號:");
? ? ? ? ? ? Arrays.sort(names);
? ? ? ? ? ? int nameNum=input.nextInt();
? ? ? ? ? ? ??
? ? ? ? ? ? if(nameNum<5){
? ? ? ? ? ? ? ? System.out.println(names[nameNum]);
? ? ? ? ? ? ? ? ??
? ? ? ? ? ? }else ?{
? ? ? ? ? ? throw new Exception();
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? ? ? ??
? ? }catch(Exception e){
? ? ??
? ? System.out.println("您查找的圖書不存在!");
continue;
? ? } ?
}while(f==1);
input.close();
} ? ? ?
}
2016-10-29
2016-10-29
是不是因為在for循環(huán)里面,判斷只有一次是正確,其他的n-1次都不對。有多次執(zhí)行else語句,拋出異常。打印那句話啊。
2016-10-28
應(yīng)該是異常拋出的使用方法有問題
2016-10-28