Java 快餐菜单(使用方法)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33272802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Fast Food Menu (using methods)
提问by Michael Romero Jr.
I'm writing a program that displays a fast food menu. The user selects an item, then enters the quantity of that item, and can continue selecting items with specific quantities until done. I have to use several methods. What I'm having trouble with is calculating a running total. This is my first Java class so I only know the basics. I put the running total in a while loop so it'll keep adding a subtotal to it, but when I call done() the runningTotal is 0. What's the best way to keep track of the running total while using multiple methods? Also, I'm open to any criticism or clean up in my code. Thank you.
我正在编写一个显示快餐菜单的程序。用户选择一个项目,然后输入该项目的数量,并可以继续选择具有特定数量的项目,直到完成。我必须使用几种方法。我遇到的问题是计算运行总数。这是我的第一个 Java 课程,所以我只知道基础知识。我将运行总计放在 while 循环中,因此它会继续向其中添加小计,但是当我调用 done() 时, runningTotal 为 0。在使用多种方法时跟踪运行总计的最佳方法是什么?此外,我愿意接受任何批评或清理我的代码。谢谢你。
import java.util.Scanner;
public class Menu {
public double subTotal;
public static double runningTotal;
private static double itemPrice;
static boolean ordering = true;
static Scanner input = new Scanner(System.in);
public static void menu() {
System.out.println("Welcome \n1. Burger (.00) \n2. Fries (.50)\n3. Soda (.00) \n4. Done");
}
public static double itemPrice(int foodItem) {
if (foodItem == 1) {
// burger= .00
System.out.println("You've ordered a burger");
itemPrice = 2.00;
}
if (foodItem == 2) {
// fries = .50
System.out.println("You've ordered fries");
itemPrice = 1.50;
}
if (foodItem == 3) {
// soda = .00
System.out.println("You've ordered a soda");
itemPrice = 1.00;
}
quantity();
return itemPrice;
}
public static double quantity() {
System.out.println("Enter quantity");
double quantity = input.nextDouble();
subTotal(quantity, itemPrice);
return quantity;
}
public static double subTotal(double quantity, double itemPrice) {
double subTotal = quantity * itemPrice;
System.out.println("Subtotal: " + subTotal);
return subTotal;
}
public static void done(double runningTotal) {
ordering = false;
System.out.println(runningTotal);
System.out.println("Enjoy your meal");
}
public static void main(String[] args) {
int menuOption;
int foodItem = 0;
input = new Scanner(System.in);
do {
double runningTotal = 0;
menu();
menuOption = input.nextInt();
switch (menuOption) {
case 1:
foodItem = 1;
itemPrice(foodItem);
break;
case 2:
foodItem = 2;
itemPrice(foodItem);
break;
case 3:
foodItem = 3;
itemPrice(foodItem);
break;
case 4:
done(runningTotal);
break;
default:
System.out.println("Invalid option.");
}
} while (ordering);
{
subTotal(quantity(), itemPrice(foodItem));
runningTotal = runningTotal + subTotal(quantity(), itemPrice(foodItem));
}
}
}
回答by YoungHobbit
You are resetting the double runningTotal=0;
in the while loop. Also the price returned by the itemPrice
needs to be added into the runningTotal
variable;
您正在double runningTotal=0;
while 循环中重置。还itemPrice
需要将返回的价格添加到runningTotal
变量中;
This is how your main method should look like. You are not required to call the subTotal
method, once the user is done.
这就是您的主要方法的样子。subTotal
一旦用户完成,您就不需要调用该方法。
public static void main(String[] args) {
int menuOption;
int foodItem = 0;
input = new Scanner(System.in);
double runningTotal=0;
do{
menu();
menuOption = input.nextInt();
switch(menuOption){
case 1:
foodItem = 1;
runningTotal += itemPrice(foodItem);
break;
case 2:
foodItem = 2;
runningTotal += itemPrice(foodItem);
break;
case 3:
foodItem = 3;
runningTotal += itemPrice(foodItem);
break;
case 4:
done(runningTotal);
break;
default:
System.out.println("Invalid option.");
}
} while(ordering);
System.out.println("Total amount: " + runningTotal);
}
Output:
输出:
Welcome
1. Burger (.00)
2. Fries (.50)
3. Soda (.00)
4. Done
1
You've ordered a burger
Enter quantity
2
Subtotal: 4.0
Welcome
1. Burger (.00)
2. Fries (.50)
3. Soda (.00)
4. Done
2
You've ordered fries
Enter quantity
1
Subtotal: 1.5
Welcome
1. Burger (.00)
2. Fries (.50)
3. Soda (.00)
4. Done
4
3.5
Enjoy your meal
Total amount: 3.5
回答by Ryan
You reset runningTotal to 0 every iteration. I have attached a working example. I moved the total calculation to your subTotal method where it adds every subTotal as they come in.
每次迭代都将 runningTotal 重置为 0。我附上了一个工作示例。我将总计算移到您的 subTotal 方法中,它会在每个 subTotal 进来时添加它们。
import java.util.Scanner;
public class menu {
public double subTotal;
public static double runningTotal;
private static double itemPrice;
static boolean ordering = true;
static Scanner input = new Scanner(System.in);
public static void menu(){
System.out.println("Welcome \n1. Burger (.00) \n2. Fries (.50)\n3. Soda (.00) \n4. Done");
}
public static double itemPrice(int foodItem) {
if (foodItem == 1) {
//burger= .00
System.out.println("You've ordered a burger");
itemPrice = 2.00;
}
if (foodItem == 2) {
//fries = .50
System.out.println("You've ordered fries");
itemPrice = 1.50;
}
if (foodItem == 3) {
//soda = .00
System.out.println("You've ordered a soda");
itemPrice = 1.00;
}
quantity();
return itemPrice;
}
public static double quantity() {
System.out.println("Enter quantity");
double quantity = input.nextDouble();
subTotal(quantity, itemPrice);
return quantity;
}
public static double subTotal(double quantity, double itemPrice) {
double subTotal = quantity*itemPrice;
System.out.println("Subtotal: "+ subTotal);
runningTotal += subTotal;
return subTotal;
}
public static void done(){
ordering = false;
System.out.println(runningTotal);
System.out.println("Enjoy your meal");
}
public static void main(String[] args) {
int menuOption;
int foodItem = 0;
input = new Scanner(System.in);
do{
double runningTotal=0;
menu();
menuOption = input.nextInt();
switch(menuOption){
case 1:
foodItem = 1;
itemPrice(foodItem);
break;
case 2:
foodItem = 2;
itemPrice(foodItem);
break;
case 3:
foodItem = 3;
itemPrice(foodItem);
break;
case 4:
done();
break;
default:
System.out.println("Invalid option.");
}
} while(ordering); {
}
}
}
回答by Rohan Tripathy
This will bring the exact output.
这将带来准确的输出。
import java.util.Scanner;
public class Resteraunt
{
public double subTotal;
public static double runningTotal;
private static double itemPrice;
static boolean ordering = true;
static Scanner input = new Scanner(System.in);
static double j=0.0;
public static void main(String[] args) {
int menuOption;
int foodItem = 0;
input = new Scanner(System.in);
double runningTotal=0;
while(ordering)
{
menu();
menuOption = input.nextInt();
switch(menuOption){
case 1:
foodItem = 1;
runningTotal += ItemPrice(foodItem);
break;
case 2:
foodItem = 2;
runningTotal += ItemPrice(foodItem);
break;
case 3:
foodItem = 3;
runningTotal += ItemPrice(foodItem);
break;
case 4:
done(runningTotal);
break;
default:
System.out.println("Invalid option.");
}
}
System.out.println("Total amount: $" + runningTotal);
}
public static void menu() {
System.out.println("Welcome \n1. Burger (.00) \n2. Fries (.50)\n3. Soda
(.00) \n4. Done");
}
public static double ItemPrice(int foodItem) {
if (foodItem == 1) {
// burger= .00
System.out.println("You've ordered a burger");
itemPrice = 2.00;
}
if (foodItem == 2) {
// fries = .50
System.out.println("You've ordered fries");
itemPrice = 1.50;
}
if (foodItem == 3) {
// soda = .00
System.out.println("You've ordered a soda");
itemPrice = 1.00;
}
quantity();
return j;
}
public static double quantity() {
System.out.println("Enter quantity");
double quantity = input.nextDouble();
subTotal(quantity, itemPrice);
return quantity;
}
public static double subTotal(double quantity, double itemPrice) {
double subTotal = quantity * itemPrice;
System.out.println("Subtotal: $" + subTotal);
j=subTotal;
return subTotal;
}
public static void done(double runningTotal) {
ordering = false;
System.out.println("Enjoy your meal");
}
}