2013-02-13 64 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"))