我正在尝试计算菜单项的总数,我无法弄清楚如何让代码获取正确的输入并将其作为项值读取。我需要它注册用户想要说2个比萨饼,每个12美元,总共24个,但我无法弄清楚。这就是我所拥有的。
package javaapplication4;
/**
*
* @author Travis
*/
import java.util.Scanner;
public class JavaApplication4 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
float Chicken = 9;
float Pizza = 12;
float Veal = 15;
float Alfredo = 9;
float Garlic = 8;
float Veggie = 7;
float Spaghetti = 9;
float Raviolli = 7;
float Meat = 8;
float Canolli = 5;
// Display menu and prices
System.out.println("1. Chicken $9");
System.out.println("2. Pizza $12");
System.out.println("3. Veal $15");
System.out.println("4. Alfredo $9");
System.out.println("5. Garlic $8");
System.out.println("6. Veggie $7");
System.out.println("7. Spaghetti $9");
System.out.println("8. Raviolli $7");
System.out.println("9. Meat $8");
System.out.println("10. Canolli 5$");
System.out.print("Enter your menu choice name: ");
Scanner input = new Scanner(System.in);
int orderNumber = input.nextInt();
//prompt user for quantity
System.out.print("How many would you like?: ");
input = new Scanner(System.in);
int orderAmount = input.nextInt();
//Switch to determine price
double price = orderNumber + orderAmount;
switch (orderNumber) {
case 1: price = Chicken ;
break;
case 2: price = Pizza;
break;
case 3: price = Veal;
break;
case 4: price = Alfredo;
break;
case 5: price = Garlic;
break;
case 6: price = Veggie;
break;
case 7: price = Spaghetti;
break;
case 8: price = Raviolli;
break;
case 9: price = Meat;
break;
case 10: price = Canolli;
break;
}
System.out.println("Your total is " + price);
System.out.println("Please enter your payment amount");
int payment = input.nextInt();
double total = payment - price;
System.out.println ("Thank you your change is $" + total);
}
答案 0 :(得分:0)
看看你的switch语句(你从来没有真正使用过它)。
你应该有这样的东西:
double price = 0.0; //You will reassign this
switch (orderNumber) {
case 1: price = Chicken ;
break;
case 2: price = Pizza;
break;
case 3: price = Veal;
break;
case 4: price = Alfredo;
break;
case 5: price = Garlic;
break;
case 6: price = Veggie;
break;
case 7: price = Spaghetti;
break;
case 8: price = Raviolli;
break;
case 9: price = Meat;
break;
case 10: price = Canolli;
break;
double total_price = price * orderAmount; //You currently have this as an addition
答案 1 :(得分:0)
你写了
double price = orderNumber + orderAmount;
我认为应该是:
double price = orderNumber * orderAmount;