第一章的作業參考修改一下,程序運行有問題,請大家指教!
?
package com.imooc.test;
import java.util.Scanner;
public class LibraryManager {
?//創建Scanner對象console
?private static Scanner console = new Scanner(System.in);
?public static void main(String[] args) {
??// TODO Auto-generated method stub
??//定義”圖書“數組
????String[] books = { "C語言", "數據結構", "匯編語言", "高數", "大學語文", "毛概" };
????while (true) {
?????System.out.println("歡迎來到圖書管理系統!");
?????System.out.println("輸入命令:1-按照名稱查找圖書;2-按照序號查找圖書");
?????String book;
?????try {
??????//取得整型命令
??????int command =console.nextInt();
??????//根據不同命令值,進行不同操作
??????switch (command) {
??????case 1://按照圖書名稱選擇圖書
???????book = getBookByName(books);
???????System.out.println("book:" + book);
???????break;//退出循環,進行循環后面的代碼
??????case 2://按照圖書序號(數組下標)選擇圖書
???????book = getBookByNumber(books);
???????System.out.println("book:" + book);
???????break;
??????/*case -1://返回值為-1,說明輸入有誤
???????System.out.println("命令輸入錯誤!請根據提示輸入數字命令!");
???????continue;//結束本次循環,直接進入下次循環*/
??????default://其他值的命令均認為是錯誤命令
???????System.out.println("命令輸入錯誤!請根據提示輸入數字命令!");
???????continue;
??????}
??????break;//退出程序
?????} catch (Exception bne) {
??????//捕獲”圖書不存在異?!皶r,要求重新輸入命令
??????System.out.println(bne.getMessage());
??????continue;
?????}
????}
???}
???//按照圖書名稱查詢圖書
???private static String getBookByName(String[] books)
?????throws Exception {
????System.out.println("輸入圖書名稱:");
????//獲取輸入的圖書名稱
????String name = console.next();
????for (int i = 0; i < books.length; i++) {
?????if (name.equals(books[i]))
??????//輸入的名稱與某一圖書名稱匹配,返回該圖書
??????return books[i];
????}
????//若無匹配,拋出”圖書不存在異?!?br />????throw new Exception("圖書不存在!");
???}
???//根據圖書序號(數組下標)查詢圖書
???private static String getBookByNumber(String[] books)
?????throws Exception {
????//while (true) {
?????System.out.println("輸入圖書序號:");
?????try {
??????//獲取輸入的圖書序號(數組下標)
??????int index = console.nextInt();
??????//若返回值為-1
??????/*if(index == -1){
???????System.out.println("命令輸入錯誤!請根據提示輸入數字命令!");
???????continue;
??????}*/
??????//若不出現”數組下標越界異常“,則返回相應位置的圖書
??????String book = books[index];
??????return book;
?????} catch (ArrayIndexOutOfBoundsException e) {
??????//輸入的序號不存在(引發”數組下標越界異?!埃?,則拋出”圖書不存在異?!?br />??????Exception bookNotExists = new Exception("圖書不存在!");
??????bookNotExists.initCause(e);
??????throw bookNotExists;
?????}
????}
??}?
2016-07-16
private static Scanner console = new Scanner(System.in);為什么要在Scanner前面加private static
2016-05-12
package com.imooc.proj_1;
import java.util.Scanner;
public class BookManagerEasy {
private static Scanner console = new Scanner(System.in);
public static void main(String[] args) {
// 定義”圖書“數組
String[] books = { "C語言", "數據結構", "匯編語言", "高數", "大學語文", "毛概" };
while (true) {
System.out.println("輸入命令:1-按照名稱查找圖書;2-按照序號查找圖書");
String book;
try {
// 取得整型命令
int command = inputCommand();
// 根據不同命令值,進行不同操作
switch (command) {
case 1:// 按照圖書名稱選擇圖書
book = getBookByName(books);
System.out.println("book:" + book);
break;
case 2:// 按照圖書序號(數組下標)選擇圖書
book = getBookByNumber(books);
System.out.println("book:" + book);
break;
case -1:// 返回值為-1,說明輸入有誤
System.out.println("命令輸入錯誤!請根據提示輸入數字命令!");
continue;
default:// 其他值的命令均認為是錯誤命令
System.out.println("命令輸入錯誤!");
continue;
}
break;// 退出程序
} catch (Exception bne) {
// 捕獲”圖書不存在異?!皶r,要求重新輸入命令
System.out.println(bne.getMessage());
continue;
}
}
}
// 按照圖書名稱查詢圖書
private static String getBookByName(String[] books) throws Exception {
System.out.println("輸入圖書名稱:");
// 獲取輸入的圖書名稱
String name = console.next();
for (int i = 0; i < books.length; i++) {
if (name.equals(books[i]))
// 輸入的名稱與某一圖書名稱匹配,返回該圖書
return books[i];
}
// 若無匹配,拋出”圖書不存在異常“
throw new Exception("圖書不存在!");
}
// 根據圖書序號(數組下標)查詢圖書
private static String getBookByNumber(String[] books) throws Exception {
while (true) {
System.out.println("輸入圖書序號:");
try {
// 獲取輸入的圖書序號(數組下標)
int index = inputCommand();
// 若返回值為-1
if (index == -1) {
System.out.println("命令輸入錯誤!請根據提示輸入數字命令!");
continue;
}
// 若不出現”數組下標越界異常“,則返回相應位置的圖書
String book = books[index];
return book;
} catch (ArrayIndexOutOfBoundsException e) {
// 輸入的序號不存在(引發”數組下標越界異?!埃瑒t拋出”圖書不存在異?!?/p>
Exception bookNotExists = new Exception("圖書不存在!");
bookNotExists.initCause(e);
throw bookNotExists;
}
}
}
// 從控制臺輸入命令,用于輸入命令和輸入圖書序號
private static int inputCommand() {
int command;
try {
command = console.nextInt();
return command;
} catch (Exception e) {
// 若輸入字符型或者字符串,則拋出異常,捕獲該異常,拋出”錯誤命令異常“
console = new Scanner(System.in);
// 返回-1
return -1;
}
}
}