2014-09-05 25 views
0

我目前正在做一個項目,必須採取一個整數1-10切換到一個雙倍然後倍數與數量雙。然而,所有這些都必須由用戶輸入,就像你從餐廳訂購了一些東西。我遇到的問題是獲得上述存儲總量(價格*數量)的循環,然後將其全部添加。 exp:1 * 5 = 5 + 4.5 * 2 = 9 =總共14個。我必須將其循環到用戶可以用輸入終止循環的點。我試圖通過向交換機添加一個計數器++來做到這一點,但我似乎沒有任何工作。以下是我正在處理的當前代碼。如何在switch語句上設置do ... while循環並添加輸入?

因此,所有的一切,下面我需要循環(直到系統提示沒有),並不斷加起來的總數,直到我在System.out.println("total");線得到一個最終的總

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 

package Menu; 
import java.util.Scanner; 
/** 
* 
* @author ------- 
*/ 


public class Menu { 


    public static void main(String[] args) { 
    Scanner input= new Scanner(System.in); 
    Scanner keyboard = new Scanner (System.in); 

    double price = 0; 
    int ItemNum; 
    int quantity; 
    double change; 
    double total; 
    double payment; 
    int counter = 1; 


    System.out.println("Welcome to Home Italliano!\nHit enter for menu."); 
    keyboard.nextLine(); 


    System.out.println("Menu" +"\n"+"(1)Noodles - 1.15$"+"\n"+"(2)Pizza Slice - 3.00$" 
     +"\n"+"(3)Lasagna - 4.99$"+"\n"+"(4)Beverage - 2.25"+"\n"+"(5)Md. Pizza - 7.85$"+"\n"+ 
     "(6)Lg. Pizza - 11.10$"+"\n"+"(7)Calzone - 3.30$"+"\n"+"(8)Garlic Knot - 1.25$" 
     +"\n"+"(9)Rg. Pasta - 8.00$"+"\n"+"(10)Triple Meat Pasta - 9.99$"); 

    while (counter ==1) 
    System.out.println("Please input the number of the item you wish to order. hit 0 for total."); 
    ItemNum = input.nextInt(); 

    System.out.println("Please enter the quantity for this item"); 
    quantity = input.nextInt(); 

    total = price * quantity; 

    switch (ItemNum){ 
     case 0: 
      counter++; 
     case 1: 
      price = 1.15; 
      break; 
     case 2: 
      price = 3.00; 
      break; 
     case 3: 
      price = 4.99; 
      break; 
     case 4: 
      price = 2.25; 
      break; 
     case 5: 
      price = 7.85; 
      break; 
     case 6: 
      price = 11.10; 
      break; 
     case 7: 
      price = 3.30; 
      break; 
     case 8: 
      price = 1.25; 
     case 9: 
      price = 8.00; 
      break; 
     case 10: 
      price = 9.99; 
      break; 
     default: 
       System.out.println("Please enter a menu item."); 

    } 

    System.out.println(total); 


     } 

      }  
+1

在switch語句之後放置一個開括號''後面的'while(counter == 1)'行和一個右括號'}'。 – 2014-09-05 11:02:34

+0

我無法看到上面的代碼是如何通過「請輸入數字...」這一行的,因爲它現在是這樣的:它看起來會不斷打印該語句。 – 2014-09-05 11:04:46

+0

您的while循環沒有括號,所以下一行是循環中唯一的一行,它永遠不會存在循環。 – 2014-09-05 11:08:49

回答

0

一些提示:

  • 附上你的邏輯在無限循環(即while(true))
  • 注意你的花括號內爲循環
  • 要麼使用SP或者使用Scanner.nextLine來獲得輸入(即,輸入)的String表示。 「Q」爲「跳槽」),並break無限循環,如果匹配(你會需要一些String - 在這種情況下>int轉換)
  • 這是「意大利語」,一個「L」
+0

我改變了整個編碼,以開始菜單前的while(true)並拋出if break;似乎工作。 – CypherThis 2014-09-05 19:20:55

+0

@CypherThis酷! – Mena 2014-09-05 19:21:32

0

你while循環沒有任何大括號:

while (counter ==1) 
0

其他人指出:你的while循環缺少大括號,因此隻影響第二行。

另請參見:您在通過switch條款確定之前訪問價格。

而如果添加括號它可能工作,你的解決方案應該看起來更像是這樣的:

1,對於在菜單中的項目創建一個私有類。這樣一個MenuItem會有一個名字和一個價格。

2,然後創建一個MenuItems的java.utils.HashMap。然後你用你的菜單啓動這張地圖。 一個Map將一個給定的密鑰(在這種情況下menuNumber)給定的對象(在本例中是MenuItem對象)

例如爲:

menuMap.put(1, new MenuItem("Noodles", Double.valueOf("1.15")); 

3,做初步的產出;

System.out.println("Menu:"); 
for(String key: menuMap.keySet()) 
{ 
    System.out.println(menuMap.get(key).toString()); 
} 

4,創建一個while循環,像這樣的

//Initially Scan for and validate ItemChoice 
while(!inputMenuNumber == 'Exit Signal') 
{ 
    //Scan for and validate Input Amount 

    MenuItem chosenItem = menuMap.get(inputMenuNumber); 
    total = chosenItem.getItemPrice() * Integer.valueOf(inputAmount); 

    //Scan for and validate ItemChoice 
} 
Sytem.out.println(total); 

這使得代碼更容易閱讀和理解。

如果您有任何疑問,請不要猶豫,問。

+0

我添加了大括號到while循環和切換後。但現在我得到System.out.println(total);因爲變量可能不是初始化,所以拋出總計錯誤。 – CypherThis 2014-09-05 17:55:57