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

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

//感覺不太好,為什么try...catch沒啥作用勒。麻煩幫我看看

package com.Home;

import java.util.Scanner;

public class rentForBook {

public static void main(String args[]){

rentForBook Rent = new rentForBook();

Rent.waysOfRent();

}

public String[] typesOfRent(){

String[] bookTypes = {"高數","數據結構"};

return bookTypes;

}

public void waysOfRent(){

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

Scanner Input = new Scanner(System.in);

int way = Input.nextInt();

try{if(way == 1){

BasedOnName();

}

if(way == 2){

BasedOnNums();

}

else{

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

}

}catch(Exception e){

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

}

}

public void BasedOnName(){

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

Scanner Input1 = new Scanner(System.in);

String name = Input1.next();

String bookTypes[] = typesOfRent();

rentForBook ifsure = new rentForBook();

try{

if(name.equals(bookTypes[0])){

ifsure.AdvancedMath();

}

if(name.equals(bookTypes[1])){

ifsure.DataConstructure();

}

else{

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

}

}catch(Exception e){

e.printStackTrace();

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

}

BasedOnName();

}

public void BasedOnNums(){

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

Scanner Input2 = new Scanner(System.in);

int nums = Input2.nextInt();

rentForBook ifsure = new rentForBook();

try{

if(nums == 1){

ifsure.AdvancedMath();

}

if(nums == 2){

ifsure.DataConstructure();

}

if(nums > 2){

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

}

}catch(Exception e){

e.printStackTrace();

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

}

BasedOnNums();

}

public void AdvancedMath(){

System.out.println("book : 高數");

System.out.println("確認請按 1!");

Scanner Input = new Scanner(System.in);

int num = Input.nextInt();

try{if(num == 1)

System.out.println("您已成功租借高數!");

}catch(Exception e){

e.printStackTrace();

System.out.println("輸入不正確,請重新輸入!");

}

}

public void DataConstructure(){

System.out.println("book : 數據結構");

System.out.println("確認請按 1!");

Scanner Input = new Scanner(System.in);

int num = Input.nextInt();

try{if(num == 1)

System.out.println("您已成功租借數據結構!");

}catch(Exception e){

e.printStackTrace();

System.out.println("輸入不正確,請重新輸入!");

}

}

}


正在回答

3 回答

再者

public?void?DataConstructure(){
System.out.println("book?:?數據結構");
System.out.println("確認請按?1!");
Scanner?Input?=?new?Scanner(System.in);
int?num?=?Input.nextInt();
try{if(num?==?1)
System.out.println("您已成功租借數據結構!");
}catch(Exception?e){
e.printStackTrace();
System.out.println("輸入不正確,請重新輸入!");
//運行到這自動就會跳出系統此步驟,闊儀加一句,goodLuck
DataConstructure();
}
}


}


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

親,try-catch應該把scanner輸入語句也加進去,否則監控不到輸入的

Scanner?Input?=?new?Scanner(System.in);
try{
????int?way?=?Input.nextInt();
????……
}catch(){
????……
}


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

你應該在cath中重新調用方法,而不是把異常打印出來,你再試試

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

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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