第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

在作業(yè)參考的基礎(chǔ)上進(jìn)行了我個(gè)人的優(yōu)化,請(qǐng)大佬們指點(diǎn)一二。


import java.util.Scanner;


public class Demo {


private static Scanner go = new Scanner(System.in);

//數(shù)組定義

static String[] books = {"西語言","西家家","數(shù)據(jù)褲"};

public static void main(String[] args){

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

Scanner in =new Scanner(System.in);

String book;

try {

int num = in.nextInt();

if(num == 1) {

//判斷用戶想要使用名稱查找圖書

book = getBookByName(books);

//獲取用戶的圖書

System.out.println("book:"+book);

}else if(num == 2) {

//判斷用戶想要用序號(hào)來查找圖書

book = getBookByNumber(books);

//獲取圖書

System.out.println("book:"+book);

}

//只要輸入的不是1或2,程序結(jié)束

else {

System.out.println("輸入不對(duì),請(qǐng)重新運(yùn)行!謝謝!");

}

//如果不是整型那么程序也結(jié)束,重新運(yùn)行

}catch(Exception e) {

System.out.println("命令輸入錯(cuò)誤!請(qǐng)根據(jù)提示輸入數(shù)字命令!請(qǐng)重新運(yùn)行!謝謝!");

}

}

//匹配名稱輸入,返回圖書名稱

public static String getBookByName(String[] book) throws Exception{

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

String bookname = go.next();

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

if(bookname.equals(books[i])){

return books[i];

}

}

throw new Exception("圖書不存在!請(qǐng)重新運(yùn)行!謝謝!");

}

//匹配序號(hào)輸入返回圖書名稱

public static String getBookByNumber(String[] book) throws Exception{

System.out.println("輸入圖書序號(hào):");

try {

//直接輸出,如果下標(biāo)越界則會(huì)報(bào)錯(cuò)

int booknumber = go.nextInt();

String book1 = books[booknumber-1];

return book1;

}catch(Exception e){

Exception ok = new Exception("圖書不存在2!請(qǐng)重新運(yùn)行!謝謝!");

throw ok;

}

}


}


正在回答

2 回答

代碼結(jié)構(gòu)很清晰,贊

0 回復(fù) 有任何疑惑可以回復(fù)我~

package weak2;

import java.util.Scanner;


public class Demo {


private static Scanner go = new Scanner(System.in);

//數(shù)組定義

static String[] books = {"西語言","西家家","數(shù)據(jù)褲"};

public static void main(String[] args){

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

Scanner in =new Scanner(System.in);

String book;

try {

int num = in.nextInt();

if(num == 1) {

//判斷用戶想要使用名稱查找圖書

book = getBookByName(books);

//獲取用戶的圖書

System.out.println("book:"+book);

}else if(num == 2) {

//判斷用戶想要用序號(hào)來查找圖書

book = getBookByNumber(books);

//獲取圖書

System.out.println("book:"+book);

}

//只要輸入的不是1或2,程序結(jié)束

else {

System.out.println("輸入不對(duì),請(qǐng)重新運(yùn)行!謝謝!");

}

//如果不是整型那么程序也結(jié)束,重新運(yùn)行

}catch(Exception e) {

System.out.println("命令輸入錯(cuò)誤!請(qǐng)根據(jù)提示輸入數(shù)字命令!請(qǐng)重新運(yùn)行!謝謝!");

}

}

//匹配名稱輸入,返回圖書名稱

public static String getBookByName(String[] book) throws Exception{

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

String bookname = go.next();

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

if(bookname.equals(books[i])){

return books[i];

}

}

throw new Exception("圖書不存在!請(qǐng)重新運(yùn)行!謝謝!");

}

//匹配序號(hào)輸入返回圖書名稱

public static String getBookByNumber(String[] book) throws Exception{

System.out.println("輸入圖書序號(hào):");

try {

//直接輸出,如果下標(biāo)越界則會(huì)報(bào)錯(cuò)

int booknumber = go.nextInt();

String book1 = books[booknumber-1];

return book1;

}catch(Exception e){

Exception ok = new Exception("圖書不存在2!請(qǐng)重新運(yùn)行!謝謝!");

throw ok;

}

}


}



0 回復(fù) 有任何疑惑可以回復(fù)我~

舉報(bào)

0/150
提交
取消

在作業(yè)參考的基礎(chǔ)上進(jìn)行了我個(gè)人的優(yōu)化,請(qǐng)大佬們指點(diǎn)一二。

我要回答 關(guān)注問題
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)