2015-02-23 84 views
-1

我正在爲我的Java類編寫一個程序,我需要製作一個類似於輸出的收據。我必須讓用戶從3項中選擇。選擇項目後,他們選擇數量,我乘以價格給他們一個總數。用戶需要挑選所有3個項目,但可以按任意順序挑選它們。我正在嘗試使用Switch/Case方法來幫助我將變量分配給用戶選擇的內容。這是我目前所做的:開關/案例Java返回主菜單

System.out.println("Enter name of First item"); 
System.out.println(" 1. Gum, 2. Soda, 3. Chips"); 
firstItem = keyboard.nextInt(); 
switch (firstItem) { 
    case 1: 
     System.out.println("Gum"); 
     System.out.println(gumPrice); 
     System.out.println("How many packs of gum would you like to purchase?"); 
     gumBought = keyboard.nextInt(); 
     break; 
    case 2: 
     System.out.println("Soda"); 
     System.out.println(sodaPrice); 
     System.out.println("How many cups of soda would you like to purchase?"); 
     sodaBought = keyboard.nextInt(); 
     break; 
    case 3: 
     System.out.println("Chips"); 
     System.out.println(chipsPrice); 
     System.out.println("How many bags of chips would you like to purchase?"); 
     chipsBought = keyboard.nextInt(); 
     break; 
    default: 
     System.out.println("Unknown Entry"); 
     break; 
} 

然後它開始第二個項目開關/案例。我需要知道如何讓它回到主要問題(口香糖/蘇打/薯片),如果用戶在1-3之外選擇一個數字或者選擇相同的數字/項目兩次。我怎樣纔能有效地做到這一點?

此外,我可以使用字符串而不是Int來允許用戶輸入「Gum」而不是「1」?謝謝。

+0

在java 7 +中,您可以使用字符串作爲開關盒 – beresfordt 2015-02-23 22:21:48

+1

您是否嘗試過使用'loop'或使用'String'作爲'switch-case'的輸入類型? – 2015-02-23 22:21:58

+0

是否可變= keyboard.nextLine(); – 2015-02-23 22:25:52

回答

0
  • 給用戶一個選擇與 '9' 退出
  • 把switch語句中while()循環

這應該工作:

public static void main(String[] args) { 

    double gumPrice, sodaPrice, chipsPrice; 

    gumPrice = 1.25; 
    sodaPrice = 4.00; 
    chipsPrice = 3.25; 

    while(true){ 
    Scanner keyboard = new Scanner(System.in); 
    System.out.println("Enter name of First item"); 
    System.out.println(" 1. Gum, 2. Soda, 3. Chips, 9. EXIT"); 
    int firstItem = keyboard.nextInt(); 

    switch (firstItem){ 
     case 1: 
      System.out.println("Gum"); 
      System.out.println(gumPrice); 
      System.out.println("How many packs of gum would you like to purchase?"); 
      int gumBought = keyboard.nextInt(); 
      break; 
     case 2: 
      System.out.println("Soda"); 
      System.out.println(sodaPrice); 
      System.out.println("How many cups of soda would you like to purchase?"); 
      int sodaBought = keyboard.nextInt(); 
      break; 
     case 3: 
      System.out.println("Chips"); 
      System.out.println(chipsPrice); 
      System.out.println("How many bags of chips would you like to purchase?"); 
      int chipsBought = keyboard.nextInt(); 
      break; 
     case 9: 
      return; 
     } 
    } 
} 
+1

刪除'loop'變量並使用while(真)' – Bohemian 2015-02-23 22:41:07

1

我很猶豫給一個完整的工作例子,所以我會給你提供片段,讓你在正確的軌道上使用Strings

首先,如果您想重複算法直到滿足條件,您可以使用loop爲您提供所搜索的內容。

while (condition) { 
    //do something 
} 

由於Java 7發佈時,switch-case可以用字符串中使用。你只需要改變你的輸入類型來匹配你希望是什麼:

System.out.print("Enter name of your item [gum] [soda] [chips]: "); 
String choice = keyboard.next().toLowerCase(); 

然後,你將不得不修改switch-case語法。

switch (choice) {//now String 
    case "gum": 
    case "soda": 
    case "chips": 
    default: 
} 

希望這會讓你開始。

+0

我收到這2個錯誤:Lab1.java:31:不兼容的類型 發現:java.lang.String中 需要:整數 \t \t開關(選擇) \t \t^ Lab1.java:63:不兼容的類型 found:java.lang.String required:int \t \t \t \t switch(choice2) \t \t \t \t^ 2錯誤 – 2015-02-24 02:07:41

+0

即時通訊使用jEdit 5。2並且已經下載了Java 8,所以我應該可以在switch語句中使用字符串 – 2015-02-24 02:34:47

+0

確保'choice'是一個字符串,並且這些個案的類型是String - 即用雙引號括起來。 – 2015-02-24 04:13:06