Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 不精确的字符匹配仍然有效_Java - Fatal编程技术网

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

Java 不精确的字符匹配仍然有效,java,Java,很抱歉这么快再次发布,但我在税务程序中遇到了另一个错误。我的程序只有在有人只输入婚姻状况的“s”、“s”、“m”、“m”、“c”或“c”时才有效。当我输入任何不是以其中一个字母开头的内容时,它会打印出无效的条目,就像它应该打印的那样,但当我输入以大写/小写字母s、m或c开头的内容时,它会继续打印,就好像我只输入了第一个字母一样。如何使程序只在输入一个字母时进行? 谢谢 这是我的密码: import java.util.Scanner; public class TaxPrep { pu

很抱歉这么快再次发布,但我在税务程序中遇到了另一个错误。我的程序只有在有人只输入婚姻状况的“s”、“s”、“m”、“m”、“c”或“c”时才有效。当我输入任何不是以其中一个字母开头的内容时,它会打印出无效的条目,就像它应该打印的那样,但当我输入以大写/小写字母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
    }
}
import java.util.Scanner;
公营税种
{
公共静态void main(字符串[]args)
{
扫描仪sc=新的扫描仪(System.in);
木炭海洋状态;
双倍总收入=0;
双倍数量=0;
双重税率=0;
双重应纳税所得额=0;
双重征税=0;
字符串anotherCustomer=“y”;
System.out.print(“A A R O N'S T A X P R E P A R\N”);
做{
系统输出打印(“您是单身,(m)结婚还是(c)同居?\n”);
System.out.print(“输入s、m或c==>”;
maritalStatus=sc.next().charAt(0);
开关(状态)
{
案例:案例:
系统输出打印(“总收入==>”;
grossIncome=sc.nextDouble();
系统输出打印(“豁免数量==>”;
NumberOfException=sc.nextInt();
应纳税收入=总收入-(1000*免税数量);
税率=20;
打破
案例“m”:案例“m”:
系统输出打印(“总收入==>”;
grossIncome=sc.nextDouble();
系统输出打印(“豁免数量==>”;
NumberOfException=sc.nextInt();
应纳税收入=总收入-(1000*免税数量);
税率=25;
打破
案例“c”:案例“c”://同居的税率取决于应纳税所得额
系统输出打印(“总收入==>”;
grossIncome=sc.nextDouble();
系统输出打印(“豁免数量==>”;
NumberOfException=sc.nextInt();
应纳税收入=总收入-(1000*免税数量);

如果(taxableIncome仅测试第一个字符,即使输入的单词较长:

maritalStatus = sc.next().charAt(0);
sc.next()
返回一个
字符串
,而
charAt(0)
仅返回其第一个字符。因此,如果输入
“sFoo”
maritalStatus
将只是
“s”
,而
“Foo”
将被忽略

您应该获得输入的整个
字符串(并可能将其转换为大写以便在
开关中进行比较)。在这种情况下,变量
maritalStatus
应声明为
字符串

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

switch (maritalStatus) {
case "S":
    //...
}
Switch语句可用于字符串,除非您使用的是Java 6或更早版本。在这种情况下,您将使用:

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

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

为了得到一个字符:我想我们使用的是Java 6,但不确定。你是说要将字符转换为字符串吗?@IronMaiden28我已经更新了我的答案。是的,
maritalStatus
应该是
String
。.toUpperCase()是什么意思do?@IronMaiden28 See-将
字符串
转换为大写,因此您只需要
大小写“S”:
而不是
大小写“S”:大小写“S”: