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

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

誰有很好的代碼,方法用的都是try-catch,而不是都用if語句,我的代碼不能按照名稱查找,有什么問題

誰有很好的代碼供參考呀,我的代碼輸入序號還可以,但是按照名稱查找就是輸不出來,輸出都是圖書不存在

public class jieshu {

String[] book={"語文","數學","英語","物理","化學","生物"};

Scanner input=new Scanner(System.in);

public void test1(){

System.out.println("輸入圖書名稱");

String name=input.next();

try {

for(int i=0;i<book.length;i++){

if(name.equals(book[i])){

System.out.println("book:"+book[i]);

}else{

} throw new Exception();

}

}catch (Exception e) {

System.out.println("圖書不存在異常");

}

}

public void test2(){

System.out.println("輸入圖書序號");

int number=input.nextInt();

try {

if(number<=book.length){

System.out.println("book:"+book[number-1]);

}else{

throw new Exception();

}

} catch (Exception e) {

System.out.println("圖書不存在");

}

}

public void test(){

System.out.println("輸入命令:1-按照名稱查找圖書;2-按照序號查找圖書");

try {

int score=input.nextInt();

if(score==1){

this.test1();

}

if(score==2){

this.test2();

}else{

throw new Exception();

}

} catch (Exception e) {

System.out.println("命令輸入錯誤,請根據提示輸入!");

this.test();

}


}

public static void main(String[] args) {

jieshu a=new jieshu();

a.test();

}

}


正在回答

5 回答

public class Books {


/**

* 要求:

* 1、定義字符串數組保存圖書信息?

* 2、提示用戶輸入,分別用"書名"和"圖書序號"查找圖書?

* 3、根據輸入信息進行適當的異常處理:

* a、如果輸入類型錯誤,拋出"錯誤命令異常",并提示重新輸入?

* b、如果書名不存在,拋出"圖書不存在異常",并提示重新輸入

* c、如果圖書序號超出字符串數組范圍,拋出"圖書不存在異常",并提示重新輸入

*?

* @param args

*?

*/

//定義數組

String[] book = new String[]{ "數學","語文","英語","歷史","地理"};

Scanner scan = new Scanner(System.in);//從控制臺接受命令

public static void main(String[] args){

Books books = new Books();

books.test();

}

/**

* 創建test方法,用于選擇查找方式

*/

public void test(){

System.out.println("請選擇查找方式:1、書名\t 2、圖書序號");

try {

Integer num = scan.nextInt();

if (num == 1) {

this.SearchName();

} else if (num == 2) {

this.SearchNum();

}else{

throw new Exception();//不匹配拋出異常

}

//捕獲異常,輸出異常,提示用戶重新輸入 ,并且調用test方法重新選擇查找方式

} catch (Exception e) {

scan = new Scanner(System.in);

System.out.println("錯誤命令! 請重新輸入");

this.test();

}

}

/**

*創建 SearchName方法,用于查找書名

*/

public void SearchName(){

System.out.println("請輸入書名!");

String title = scan.next();//接收控制臺輸入的書名

try {

//定義一個j變量用于記錄循環次數

int j = 0;

//for循環遍歷數組

for (int i = 0; i < book.length; i++) {

if (title.equals(book[i])) {

System.out.println("這是你要找的書籍:" + book[i]);

break;?

}

j++;

}

//判斷j變量是否等于數組長度,等于則拋出異常

if (j == book.length) {

throw new Exception();

}

//捕獲異常,輸出異常,提示用戶重新輸入 ,并且調用SearchName方法重新接收控制臺輸入的數據

} catch (Exception e) {

System.out.println("圖書不存在異常,請重新輸入");

this.SearchName();

}

}

/**

*創建 SearchNum方法,用于查找數組中的圖書序號

*/

public void SearchNum(){

System.out.println("請輸入圖書序號!");

//定義一個num變量用于接受控制臺輸入的數據

int num = scan.nextInt();

int j=0;

try {

//for循環遍歷數組

for (int i = 0; i < book.length; i++) {

//判斷num是否等于i,等于則輸出數組中i的位置上的元素

if (num == i) {

System.out.println("這是你要找的書籍:" + book[i]);

break;

}

//定義一個j變量用于記錄循環次數

j++;

}

//判斷j變量是否等于數組長度,等于則拋出異常

if(j == book.length){

throw new Exception();

}

//捕獲異常,輸出異常,提示用戶重新輸入 ,并且調用SearchNum方法重新接收控制臺輸入的數據

} catch (Exception e) {

System.out.println("圖書不存在異常,請重新輸入");

this.SearchNum();

}

}

}



這是我寫的 ? 你參考一下

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

wl1224500 提問者

恩恩謝謝,這個可以用
2015-03-15 回復 有任何疑惑可以回復我~
#2

sun_haha

scan = new Scanner(System.in);為什么不寫這句,調用test()會進入死循環?
2015-08-02 回復 有任何疑惑可以回復我~
Java第三季1-9經驗總結-----源碼

package com.java;

import java.util.InputMismatchException;

import java.util.Scanner;


public class LibrarySystem {


/**1.定義字符串數組保存圖書信息

* 2.提示用戶輸入,分別按“書名”和“圖書序號”查找圖書

* 3.根據輸入信息進行適當的異常處理

* ? a、如果輸入類型錯誤,拋出“錯誤命令異?!保⑻崾局匦螺斎?/p>

* ? b、如果書名不存在,拋出“圖書不存在異?!?,并提示重新輸入

* ? c、如果圖書序號超過字符串數組范圍,拋出“圖書不存在異?!?,

* ? ? ?并提示重新輸入

* @param newChoice?

* @param args

*/

static int choice;

static int bookNum;//輸入的圖書序號

static String bookName;//輸入的圖書名稱

//public static String[][] Book={{"數據結構","1"},{"軟件設計","2"},{"高等數學","3"},{"網絡安全","4"}};

public static String[][] Book=new String[4][4];

//static Scanner input=new Scanner(System.in);

public static void main(String[] args) throws Exception {

? ? ? ?bookMaintenance();

? ? ? ?bookShow();

? for(;;){

? findBook();

? }

}

public static void findBook(){ ? ?

? ? choiceInput(choice);

? ? ? ? switch(choice){

? ? ? ? case 1: ??

? ? ? ? bookNameInput();

? ? ? ? for(int i=0;i<Book[0].length;i++){

? ? ? ? if (bookName.equals(Book[0][i])){

? ? ? ? System.out.println("book:"+Book[0][i]);

? ? ? ? break;

? ? ? ? }

? ? ? ? else{

? ? ? ? if(i+1==(Book[0].length)){

? ? ? ? ? ?System.out.println("您查找的圖書不存在!");

? ? ? ? }

? ? ? ? }

? ? ? ? }

? ? ? ? ? ?break; ?

? ? ? ? case 2:

? ? ? ? bookNumInput(bookNum);

? ? ? ? for(int i=0;i<Book[1].length;i++){

? ? ? ? if (bookNum==Integer.parseInt(Book[1][i])){

? ? ? ? System.out.println("book:"+Book[0][i]);

? ? ? ? }

? ? ? ? else{

? ? ? ? if(i+1==Book[1].length){

? ? ? ? ? ?System.out.println("您查找的圖書不存在!");

? ? ? ? }

? ? ? ? }

? ? ? ? }

? ? ? ? ? ?break;?

? ? ? ? }

}


? ? public static int choiceInput(int newChoice){ ? ?

? ? boolean m = false;

outer:

? ? do{ ?

try{

? ? System.out.println("輸入命令:1-按照名稱查找圖書;2-按照序號查找圖書");

? ? Scanner input=new Scanner(System.in);

? ? int temp=input.nextInt();

? ? m=isNumeric(Integer.toString(temp));

? ? if(m){

? ? ? ?newChoice=temp;

? ? }

}catch(InputMismatchException e){

System.out.println("命令輸入錯誤!");

if(newChoice==0){

newChoice+=1;

System.out.println("系統將默認按書名進行圖書查找!");

? ? break outer; ? ?

? ? }

}finally{

new LibrarySystem();

}

? ? }while(!m&&(newChoice!=1|newChoice!=2));

choice=newChoice;

return choice;

}


public static void bookNameInput(){

outer:

? ? do{

try{

? ? System.out.println("輸入圖書名稱:");

? ? Scanner input=new Scanner(System.in);

bookName=input.next();

if(bookName==null){

? ?continue outer;

}

}catch(Exception e){

? ? ? ? System.out.println("圖書不存在異常!請輸入正確的圖書名稱!");?

? ? ? ? if(bookName==null){

? ?continue outer;

? ? ? ? ? ?}

}

? ? }while(bookName==null);

? ? }

? ? public static int bookNumInput(int newbookNum){ ??

? ? boolean m = false;

? ? outer:

? ? do{ ?

? ? try{

? ? ? ? System.out.println("輸入圖書序號:");

? ? ? ? Scanner input=new Scanner(System.in);

? ? newbookNum=input.nextInt(); ? ?

? ? m=isNumeric(Integer.toString(newbookNum));

? ? if(newbookNum<=0||newbookNum>=Book[1].length){

new LibrarySystem();

continue outer; ?

}

}catch(InputMismatchException e){

System.out.println("命令輸入錯誤!請根據提示重新輸入數字命令!");

if(m==false){

new LibrarySystem();

continue outer; ? ?

? ? }

}

? ? }while(!m&&(newbookNum<=0||newbookNum>=Book[1].length));

? ? bookNum=newbookNum;

? ? return bookNum;

? ? }

? ? public static boolean isNumeric(String str){

? ? ?if(str.matches("\\d *")){

? ? ? return true;

? ? ?}else{

? ? ? return false;

? ? ?}

? ? }

? ??

? ? public static void bookMaintenance(){

? ? Book[0][0]="數據結構";

? ? Book[0][1]="軟件設計";

? ? Book[0][2]="高等數學";

? ? Book[0][3]="網絡安全";

? ? Book[1][0]="1";

? ? Book[1][1]="2";

? ? Book[1][2]="3";

? ? Book[1][3]="4";

? ? }

? ? public static void bookShow(){

? ? System.out.println("序號 ? ? ? ? 書名");

? ? System.out.println(" "+Book[1][0]+" ? "+Book[0][0]);

? ? System.out.println(" "+Book[1][1]+" ? "+Book[0][1]);

? ? System.out.println(" "+Book[1][2]+" ? "+Book[0][2]);

? ? System.out.println(" "+Book[1][3]+" ? "+Book[0][3]);

? ? }

}

Java第三季1-9經驗總結-----源碼

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

public void searchName(){

System.out.println("輸入圖書名稱");

String name=input.next();

try {

int j=0;

for(int i=0;i<book.length;i++){

if(name.equals(book[i])){

System.out.println("book:"+book[i]);

break;

}

j++;

}

if (j == book.length) {

throw new Exception();

}

}catch (Exception e) {

System.out.println("圖書不存在異常");

}

}

我試過了,還是不行,不過謝謝你!結果是:

輸入圖書名稱

語文

圖書不存在異常

命令輸入錯誤,請根據提示輸入!

輸入命令:1-按照名稱查找圖書;2-按照序號查找圖書


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

try {

//定義一個j變量用于記錄循環次數

int j = 0;

//for循環遍歷數組

for (int i = 0; i < book.length; i++) {

if (title.equals(book[i])) {

System.out.println("這是你要找的書籍:" + title);

//用break語句跳出循環

break; ?

}

j++;

}

//判斷j變量是否等于數組長度,等于則拋出異常

if (j == book.length) {

throw new Exception();

}

//捕獲異常,輸出異常,提示用戶重新輸入 ,并且調用SearchName方法重新接收控制臺輸入的數據

} catch (Exception e) {

System.out.println("圖書不存在異常,請重新輸入");

this.SearchName();

}


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

wl1224500 提問者

我試過了,還是不行,不過謝謝你!結果是: 輸入圖書名稱 語文 圖書不存在異常 命令輸入錯誤,請根據提示輸入! 輸入命令:1-按照名稱查找圖書;2-按照序號查找圖書
2015-03-15 回復 有任何疑惑可以回復我~

因為你那for循環那里只循環了一次 ?也就是說 ?當你輸入 數學 時 ?第一次循環i=0,if判斷name和book[0]的元素不匹配,它就直接進入了else中的代碼塊,也就是拋出異常! ?所以你這按照書名查找的代碼最多能只能輸出 語文 這個元素!

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

wl1224500 提問者

那怎么改,我輸入的英語也還是英語
2015-03-15 回復 有任何疑惑可以回復我~
#2

wl1224500 提問者

我輸入的語文,結果還是語文,圖書不存在異常,可是實際上存在呀
2015-03-15 回復 有任何疑惑可以回復我~
#3

天藍色的彼岸_123 回復 wl1224500 提問者

在控制臺輸入中文時,我們只能從剛剛寫的代碼中將需要輸入的書籍名稱進行復制,粘貼到控制臺中才可以~~~
2015-05-04 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

誰有很好的代碼,方法用的都是try-catch,而不是都用if語句,我的代碼不能按照名稱查找,有什么問題

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

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

幫助反饋 APP下載

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

公眾號

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