修改之后的
/*
?* 定義個字符串數組,保存圖書信息
?*
?*/
import java.util.Scanner;
public class BookManeger {
public static void main(String[] args) {
String[] books = {"C語言","計算機網絡","Java數據庫高級教程","編程之美","英語四六級"};
BookManeger myBook = new BookManeger();
Scanner sc = new Scanner(System.in);
while(true){
System.out.println("輸入命令:1-輸入圖書名稱查詢,2-輸入圖書序號查詢");
int m = inputNum();
try{
switch(m){?
case 1:
System.out.println("請輸入圖書名稱:");
String str = (new Scanner(System.in)).next();
myBook.findBooks(str, books);
break;
case 2:
System.out.println("請輸入圖書序號:");
int num = (new Scanner(System.in)).nextInt();
myBook.findBooksName(num, books);
break;
default:
System.out.println("輸入命令錯誤:");
continue;
}
break;
}catch(Exception e){
System.out.println(e.getMessage());
continue;
}
}
}
//輸入圖書名稱進行查詢
public void findBooks(String input,String books[]) throws Exception
{
for ( int i=0; i<books.length; i++){
if ( input.equals(books[i]) ){
System.out.println("圖書名稱為:"+books[i]);
return;
}
}
throw new Exception("哈哈,您輸入的書本不存在!!");
}
//輸入圖書序號查詢
public void findBooksName(int input,String books[]) throws Exception
{
if ( input<books.length && input >=0){
System.out.println("對應的圖書為:"+books[input]);
}
else{
throw new Exception("請輸入正確的圖書序號,不存在此序號的圖書");
}
}
public static int inputNum()
{
try {
int k = new Scanner(System.in).nextInt();
return k;
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("請按照提示,輸入正確的指令");
int ?k = new Scanner(System.in).nextInt();
return k;
}
}
}
2014-12-25
public static int inputNum(){
while(true){
try {
int k = new Scanner(System.in).nextInt();
return k;
} catch (Exception e) {
System.out.println("請按照提示,輸入正確的指令");
}continue;
}
}
最后這個修正輸入格式這個方法,只能操作兩次,假如兩次都輸入不符合int的字符,則該系統結束
因此用while循環 ?不斷的進入try塊,知道輸入符合Int的為止
2014-12-05
你的那個findBooksName里面return 是跳出循環嗎?還有那個switch語句外面那個break要了沒用啊