2013-02-13 61 views
1

我的代码现在检查他们的用户是否输入“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(); 
    } 

} 
+0

定义“它不会工作” – smk 2013-02-13 03:54:05

+0

你不会在任何地方抛出错误... – kaysush 2013-02-13 03:54:13

+1

那还没有一个正确的括号......不知道这是你的意图。无论哪种方式,你能否缩小这个问题的范围? – Makoto 2013-02-13 04:00:04

回答

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(); 
     } 
    } 

} 
+2

您提供了一个巨大的代码块,没有任何解释。除非我花时间阅读OP和代码,否则它并不能说明实际问题。 – Makoto 2013-02-13 03:57:48

+0

我认为被更改的行是'if(!customerType.equalsIgnoreCase(“R”)&&!customerType.equalsIgnoreCase(“C”))' – Floris 2013-02-13 03:58:10

+0

@Makoto:抱歉,我刚刚编辑了我的答案。 – 2013-02-13 04:00:53

1

在这行代码:

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

引发错误,如果输入的是不是一个R.你也不想被抛出一个错误如果输入的是T.所以,改变 如果(!customerType.equalsIgnoreCase( “R”))

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