不准确的字符匹配仍然有效

时间:2015-10-31 20:39:17

标签: java

很抱歉这么快就发布了,但我的税务计划中遇到了另一个错误。我的节目只有在有人进入时才能工作,' ' S,' '米,' ' 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
    }
}

1 个答案:

答案 0 :(得分:3)

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

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

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

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

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

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

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

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

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