簡體   English   中英

輸入驗證Java

[英]Input Validation Java

我對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;

您需要在示例中默認設置的do-while循環中包含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的“繼續解決方案:它將開始外部do-loop的下一次迭代,因此基本上重新啟動您的程序。如果您有更多具有用戶輸入的Sections,則可能不是您想要的,如果想做愛等

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM