[英]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.