[英]How to properly use this while loop within my method to have user retry input to a maximum of three times
[英]In the java programming, if the input validation fails, how can I allow user to retry three times before throwing an exception and terminating?
java編程中,如果輸入驗證失敗,如何讓用戶重試3次才拋出異常並終止? •
Sample Run
Enter taxable income ... 80
The taxable income must be at least $1200.0
Enter taxable income ... 890
The taxable income must be at least $1200.0
Enter taxable income ... 1090
The taxable income must be at least $1200.0
Exception in thread "main" java.lang.RuntimeException: Sorry
you're having trouble
使用循環,其中
tries counter
tries counter
將大於預定義的最大拋出運行時異常與您的消息。謝謝大家。 @pshemo 你是對的,這是一項家庭作業,所以如果第三次嘗試后輸入驗證失敗,我必須強制使用“ToolBox.crash”方法來終止程序。 我按照你的建議解決了這個問題。 我在循環內初始化了一個循環計數器。 這是計算個人所得稅的程序的完整代碼。
import java.util.Scanner;
import java.io.PrintStream;
import type.lib.ToolBox;
public class L5B
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
PrintStream output = System.out;
final double MIN_AMOUNT = 1200.0;
final double BASIC_AMOUNT = 43561.0;
final double LEVEL_ONE = 43561.0 + 43562.0;
final double LEVEL_TWO = LEVEL_ONE + 47931.0;
final double TWENTY_ONE = 21/100.0;
final double THIRTY_THREE = 33/100.0;
final double THIRTY_EIGHT = 38/100.0;
final double HUNDRED = 100.0;
final double FORTY_TWO = 42/100.0;
final int THREE = 3;
final int TWO = 2;
output.print("Enter taxable income ... ");
double income;
int i = 0;
for(income = input.nextDouble(); income < MIN_AMOUNT; )
{
output.println("The taxable income must be at least " + MIN_AMOUNT);
output.print("Enter taxable income ... ");
income = input.nextDouble();
i++;
if(i >= TWO)
{
ToolBox.crash(true,"Sorry you're having trouble.");
}
}
if (income <= BASIC_AMOUNT)
{
double totalTax = income * TWENTY_ONE;
double averageRate = TWENTY_ONE * HUNDRED;
double marginalRate = averageRate;
output.printf("Tax = %,.2f", totalTax);
output.printf("\nAverage Rate = %.1f%%", averageRate);
output.printf("\nMarginal Rate = %.1f%%", marginalRate);
}
else if (income > BASIC_AMOUNT && income <= LEVEL_ONE)
{
double totalTax = BASIC_AMOUNT * TWENTY_ONE + ((income - BASIC_AMOUNT)*THIRTY_THREE);
double averageRate = HUNDRED*totalTax/income;
double marginalRate = THIRTY_THREE*HUNDRED;
output.printf("Tax = %,.2f", totalTax);
output.printf("\nAverage Rate = %.1f%%", averageRate);
output.printf("\nMarginal Rate = %.1f%%", marginalRate);
}
else if (income > LEVEL_ONE && income <= LEVEL_TWO)
{
double totalTax = BASIC_AMOUNT * TWENTY_ONE + (LEVEL_ONE - BASIC_AMOUNT) * THIRTY_THREE + ((income - LEVEL_ONE) * THIRTY_EIGHT);
double averageRate = totalTax*HUNDRED/income;
double marginalRate = THIRTY_EIGHT*HUNDRED;
output.printf("Tax = %,.2f", totalTax);
output.printf("\nAverage Rate = %.1f%%", averageRate);
output.printf("\nMarginal Rate = %.1f%%", marginalRate);
}
else if (income > LEVEL_TWO)
{
double totalTax = BASIC_AMOUNT * TWENTY_ONE + ((LEVEL_ONE - BASIC_AMOUNT) * THIRTY_THREE) + ((LEVEL_TWO - LEVEL_ONE) * THIRTY_EIGHT) + ((income - LEVEL_TWO) * FORTY_TWO);
double averageRate = totalTax*HUNDRED/income;
double marginalRate = FORTY_TWO * HUNDRED;
output.printf("Tax = %,.2f", totalTax);
output.printf("\nAverage Rate = %.1f%%", averageRate);
output.printf("\nMarginal Rate = %.1f%%", marginalRate);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.