我对Java很新。我正在做一个程序,根据一个人的婚姻状况计算所得税汇总。除了一个问题,我的程序还可以工作:我的婚姻状况输入验证部分工作不正常。当我输入s,m,M,c和C以外的东西时,该程序应该为此执行输入验证,但是目前,当我输入类似x的东西时,它会跳过总收入和豁免,并吐出税率,欠税和应纳税所得均为0。
编辑:对不起,如果那令人困惑。基本上,如果用户使用无效输入,然后用户输入有效的内容,我想知道如何让它回到switch语句的开头以确定税率任何帮助将不胜感激!import java.util.Scanner;
public class TaxPrep
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
char maritalStatus;
double grossIncome = 0;
double numberOfExemptions = 0;
double taxRate = 0;
double taxableIncome = 0;
double taxesOwed = 0;
String anotherCustomer = "y";
System.out.print("_ _ _ _ _ ' S T A X P R E P A R E R\n\n");
do{
System.out.print("Are you (s)ingle, (m)arried, or (c)ohabiting?\n");
System.out.print("Enter s, m, or c ==> ");
maritalStatus = sc.next().charAt(0);
switch (maritalStatus)
{
case 's': case 'S':
System.out.print("Gross income ==> ");
grossIncome = sc.nextDouble();
System.out.print("Number of exemptions ==> ");
numberOfExemptions = sc.nextInt();
taxableIncome = grossIncome - (1000 * numberOfExemptions);
taxRate = 20;
break;
case 'm': case 'M':
System.out.print("Gross income ==> ");
grossIncome = sc.nextDouble();
System.out.print("Number of exemptions ==> ");
numberOfExemptions = sc.nextInt();
taxableIncome = grossIncome - (1000 * numberOfExemptions);
taxRate = 25;
break;
case 'c': case 'C': //tax rate for cohabiting depends on taxable income
System.out.print("Gross income ==> ");
grossIncome = sc.nextDouble();
System.out.print("Number of exemptions ==> ");
numberOfExemptions = sc.nextInt();
taxableIncome = grossIncome - (1000 * numberOfExemptions);
if (taxableIncome <= 20_000)
{
taxRate = 10;
break;
}
else if (taxableIncome <= 50_000)
{
taxRate = 15;
break;
}
else
{
taxRate = 30;
break;
}
default: //if input for marital status is invalid
do{ //continues to ask for valid input until user inputs a valid marital status
System.out.print("\nInvalid entry.");
System.out.print("\nAre you (s)ingle, (m)arried, or (c)ohabiting?");
System.out.print("\nEnter s, m, or c ==> ");
maritalStatus = sc.next().charAt(0);
} while (maritalStatus != 's' && maritalStatus != 'S' && maritalStatus != 'm' && maritalStatus != 'M' && maritalStatus != 'c' && maritalStatus != 'C');
}
taxesOwed = taxableIncome * (taxRate / 100);
//taxable income and taxes owed cannot be negative
if (taxableIncome < 0)
{
taxableIncome = 0;
}
if (taxesOwed < 0)
{
taxesOwed = 0;
}
//tax summary
System.out.print("\nINCOME TAX SUMMARY");
System.out.print("\ntax rate: " + taxRate + "%");
System.out.print("\ntaxable income: $" + taxableIncome);
System.out.print("\ntaxes owed: $" + taxesOwed);
//would you like to process another customer?
System.out.print("\n\nProcess another customer? (y/n): ");
anotherCustomer = sc.next();
System.out.print("\n");
} while (anotherCustomer.equalsIgnoreCase("y")); //as long as user enters 'y' or 'Y', the program will continue to calculate the income tax summary
}
}
答案 0 :(得分:1)
而不是在
之后重试default: //if input for marital status is invalid
do{ //continues to ask for valid input until user inputs a valid marital status
} while(...);
只需跳到外循环的末尾:
default:
System.out.print("\nInvalid entry.");
continue;
答案 1 :(得分:0)
你需要在你的例子中默认获得的do-while-loop中有switch-Statement,比如
boolean martialStatusValid = true
maritalStatus = sc.next().charAt(0);
do {
martialStatusValid = true;
switch (maritalStatus) {
//your cases here
default:
martialStatusValid = false;
//display some kind of error message ...
}
} while (! martialStatusValid)
这样,只要martialStatus无效,系统就会提示用户输入新内容,然后重复切换。
有一点值得注意的是Launes&#34; continue-solution:这将启动外部do循环的下一次迭代,所以基本上重启你的程序。如果您有更多具有用户输入的章节,那么可能不是您所追求的。如果你想要求性生活等。