亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

修改之后的

/*

?* 定義個字符串數組,保存圖書信息

?*

?*/

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;

}

}

}



正在回答

2 回答

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的為止


0 回復 有任何疑惑可以回復我~

你的那個findBooksName里面return 是跳出循環嗎?還有那個switch語句外面那個break要了沒用啊

0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

修改之后的

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號