gpt4 book ai didi

java - 不精确的字符匹配仍然有效

转载 作者:行者123 更新时间:2023-11-29 04:56:54 26 4
gpt4 key购买 nike

很抱歉这么快又发帖了,但我在税务程序中遇到了另一个错误。只有当有人只输入“s”、“S”、“m”、“M”、“c”或“C”来表示婚姻状况时,我的程序才能正常工作。当我输入任何不以这些字母之一开头的内容时,它会打印 Invalid Entry,就像它应该的那样,但是当我输入以大写/小写字母 s、m 或 c 开头的内容时,它会继续进行,就好像我只输入第一个字母。我如何才能使程序仅在您输入一个字母时才继续?谢谢!!

这是我的代码:

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("A A R O N ' 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
System.out.print("\nInvalid entry.\n");
continue;
}

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
}
}

最佳答案

您只测试第一个字符,即使输入的单词更长:

maritalStatus = sc.next().charAt(0);

sc.next() 返回一个 StringcharAt(0) 只返回它的第一个字符。因此,如果您输入 "sFoo"maritalStatus 将只是 's'"Foo" 将是忽略。

您应该得到整个输入的 String(并可能将其转换为大写以便于在 switch 中进行比较)。在这种情况下,变量 maritalStatus 应声明为 String:

maritalStatus = sc.next().toUpperCase();

switch (maritalStatus) {
case "S":
//...
}

除非您使用的是 Java 6 或更早版本,否则 Switch 语句适用于字符串。在这种情况下,您将使用:

maritalStatus = sc.next().toUpperCase();

if (maritalStatus.equals("S")) {
//...
} else if...

关于java - 不精确的字符匹配仍然有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33456371/

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