[英]How to set a do…while loop on a switch statement and add inputs?
我目前正在開發一個項目,該項目必須使用1-10的整數將其切換為兩倍,然后將其乘以倍數。 但是,所有這些都必須由用戶輸入,就像您要從餐廳訂購東西一樣。 我遇到的問題是得到上面的循環,該循環存儲總計(價格*數量),然后將其總計。 exp:1 * 5 = 5 + 4.5 * 2 = 9 =總共14。我必須將此循環到一個點,以便用戶可以用輸入終止循環。 我嘗試通過在交換機上添加一個counter ++來實現此目的,但我似乎無濟於事。 以下是我正在處理的當前代碼。
因此,總而言之,我需要在下面循環(直到提示不要這樣做),並不斷累加總計,直到在System.out.println("total");
獲得最終的總計為止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);
}
}
一些提示:
while(true))
。 int
或使用Scanner.nextLine
獲取輸入的String
表示形式(即,“ q”表示“ quit”),如果匹配則break
無限循環(在這種情況下,您將需要一些String
> int
轉換) 您的while循環沒有任何花括號:
while (counter ==1)
正如其他人指出的那樣:您的while循環缺少花括號,因此僅影響第二行。
另外:在通過switch子句確定價格之前,您正在訪問價格。
如果添加花括號可能會起作用,但是您的解決方案應如下所示:
1,為菜單上的項目創建一個私有類。 這樣的MenuItem將具有名稱和價格。
2,然后創建一個MenuItems的java.utils.HashMap。 然后,使用菜單初始化此地圖。 Map將給定的Key(在本例中為menuNumber)映射到給定的Object(在本例中為MenuItem Object)
例如:
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);
它使代碼更易於閱讀和理解。
如有任何疑問,請隨時提出。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.