我目前正在做一個項目,必須採取一個整數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);
}
}
在switch語句之後放置一個開括號''後面的'while(counter == 1)'行和一個右括號'}'。 – 2014-09-05 11:02:34
我無法看到上面的代碼是如何通過「請輸入數字...」這一行的,因爲它現在是這樣的:它看起來會不斷打印該語句。 – 2014-09-05 11:04:46
您的while循環沒有括號,所以下一行是循環中唯一的一行,它永遠不會存在循環。 – 2014-09-05 11:08:49