gpt4 book ai didi

java - 输入验证 Java

转载 作者:行者123 更新时间:2023-12-01 10:59:13 24 4
gpt4 key购买 nike

我对 Java 还很陌生。我正在编写一个程序,根据一个人的婚姻状况计算所得税摘要。我的程序可以正常工作,但有一个问题:我的婚姻状况输入验证部分无法正常工作。当我输入 s、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
}
}

最佳答案

而不是稍后重试

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;

关于java - 输入验证 Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33455158/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com