我必須確保包含處理異常的語句,但是當我這樣做時,變量Amount不會改變。幫幫我?變量不會改變
public static void main(String[] args){
AmwayTickets run = new AmwayTickets();
System.out.print(run.ticketAmount());
}
public int ticketAmount(){
System.out.println("Enter the amount of tickets you wish to purchase: ");
int amount = 0;
try {
amount = keyboard.nextInt();
}
catch (InputMismatchException e){
System.out.println("Invalid Amount");
ticketAmount();
return amount;
}
if (amount < 0){
System.out.println("Invalid Amount");
ticketAmount();
return amount;
}
return amount;
}
你想讓它改變嗎?如果nextInt拋出一個異常,它是否應該改變,意味着出了什麼問題? – dchhetri
你在問什麼?詳細說明你想要做什麼,你期望什麼,什麼讓你困惑。 – EdC