验证两个字符串之间的用户输入?

时间:2013-02-13 03:52:08

标签: java

我的代码现在检查他们是否在“r”中为客户类型键入用户类型,如果不是它会抛出错误消息,我希望它还检查用户是否键入“c”,因为这也是有效的客户类型。我尝试在第一个“if”之后使用“else if”语句,所以我可以检查它是否不是r然后是c如果不抛出错误信息但它不会工作????

public static void main(String[] args)
{
    Scanner sc = new Scanner(System.in);
    String choice = "y";

    while (!choice.equalsIgnoreCase("n"))
    {
        // get the input from the user
        System.out.print("Enter customer type (r/c): ");
        String customerType = sc.next();
        if (!customerType.equalsIgnoreCase("R"))
        {
            sc.nextLine();
        System.out.println("Error! Invalid Customer Type. Try Again ");
        continue;
        }
        else




        System.out.print("Enter subtotal:   ");
        double subtotal = sc.nextDouble();

        // get the discount percent
        double discountPercent = 0;
        if (customerType.equalsIgnoreCase("R"))
        {
            if (subtotal < 100)
                discountPercent = 0;
            else if (subtotal >= 100 && subtotal < 250)
                discountPercent = .1;
            else if (subtotal >= 250)
                discountPercent = .2;
        }
        else if (customerType.equalsIgnoreCase("C"))
        {
            if (subtotal < 250)
                discountPercent = .2;
            else
                discountPercent = .3;
        }
        //else

        //{sc.nextLine();
        //System.out.println("Error! Invalid Customer Type. Try Again ");
        //continue;
        //}
        //else}
       // {
          //  discountPercent = .1;
       // }

        // calculate the discount amount and total
        double discountAmount = subtotal * discountPercent;
        double total = subtotal - discountAmount;

        // format and display the results
        NumberFormat currency = NumberFormat.getCurrencyInstance();
        NumberFormat percent = NumberFormat.getPercentInstance();
        System.out.println(
                "Discount percent: " + percent.format(discountPercent) + "\n" +
                "Discount amount:  " + currency.format(discountAmount) + "\n" +
                "Total:            " + currency.format(total) + "\n");

        // see if the user wants to continue
        System.out.print("Continue? (y/n): ");
        choice = sc.next();
        System.out.println();
    }

}

2 个答案:

答案 0 :(得分:1)

好吧,如果我不想错过你的问题,你需要验证用户是否只输入客户类型的r和c。

所以,只需在if语句中添加另一个条件。

试试这个:

public static void main(String[] args)
{
    Scanner sc = new Scanner(System.in);
    String choice = "y";

    while (!choice.equalsIgnoreCase("n"))
    {
        // get the input from the user
        System.out.print("Enter customer type (r/c): ");
        String customerType = sc.next();

        // VALIDATE ONLY R and C customer type.
        if (!customerType.equalsIgnoreCase("R") && !customerType.equalsIgnoreCase("C"))
        {
            sc.nextLine();
        System.out.println("Error! Invalid Customer Type. Try Again ");
        continue;
        }
        else {

        System.out.print("Enter subtotal:   ");
        double subtotal = sc.nextDouble();

        // get the discount percent
        double discountPercent = 0;
        if (customerType.equalsIgnoreCase("R"))
        {
            if (subtotal < 100)
                discountPercent = 0;
            else if (subtotal >= 100 && subtotal < 250)
                discountPercent = .1;
            else if (subtotal >= 250)
                discountPercent = .2;
        }
        else if (customerType.equalsIgnoreCase("C"))
        {
            if (subtotal < 250)
                discountPercent = .2;
            else
                discountPercent = .3;
        }
        //else

        //{sc.nextLine();
        //System.out.println("Error! Invalid Customer Type. Try Again ");
        //continue;
        //}
        //else}
       // {
          //  discountPercent = .1;
       // }

        // calculate the discount amount and total
        double discountAmount = subtotal * discountPercent;
        double total = subtotal - discountAmount;

        // format and display the results
        NumberFormat currency = NumberFormat.getCurrencyInstance();
        NumberFormat percent = NumberFormat.getPercentInstance();
        System.out.println(
                "Discount percent: " + percent.format(discountPercent) + "\n" +
                "Discount amount:  " + currency.format(discountAmount) + "\n" +
                "Total:            " + currency.format(total) + "\n");

        // see if the user wants to continue
        System.out.print("Continue? (y/n): ");
        choice = sc.next();
        System.out.println();
        }
    }

}

答案 1 :(得分:1)

在这些代码行中:

if (!customerType.equalsIgnoreCase("R"))
{
        sc.nextLine();
    System.out.println("Error! Invalid Customer Type. Try Again ");
    continue;
}

如果输入不是R,则抛出错误。如果输入是T,则也不希望抛出错误。所以,更改     if(!customerType.equalsIgnoreCase(“R”))

if (!customerType.equalsIgnoreCase("R") && !customerType.equalsIgnoreCase("T"))