修改之后的
/*
?* 定義個(gè)字符串?dāng)?shù)組,保存圖書信息
?*
?*/
import java.util.Scanner;
public class BookManeger {
public static void main(String[] args) {
String[] books = {"C語言","計(jì)算機(jī)網(wǎng)絡(luò)","Java數(shù)據(jù)庫高級教程","編程之美","英語四六級"};
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("輸入命令錯(cuò)誤:");
continue;
}
break;
}catch(Exception e){
System.out.println(e.getMessage());
continue;
}
}
}
//輸入圖書名稱進(jìn)行查詢
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("對應(yīng)的圖書為:"+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;
}
}
最后這個(gè)修正輸入格式這個(gè)方法,只能操作兩次,假如兩次都輸入不符合int的字符,則該系統(tǒng)結(jié)束
因此用while循環(huán) ?不斷的進(jìn)入try塊,知道輸入符合Int的為止
2014-12-05
你的那個(gè)findBooksName里面return 是跳出循環(huán)嗎?還有那個(gè)switch語句外面那個(gè)break要了沒用啊