Java快餐菜单(使用方法)
我正在写一个显示快餐菜单的程序。用户选择一个项目,然后输入该项目的数量,并可以继续选择具有特定数量的项目,直到完成。我不得不使用几种方法。我遇到的问题是计算一个运行总数。这是我的第一个Java类,所以我只知道基础知识。我将running total放在while循环中,这样它将继续向其中添加小计,但当我调用done()时,runningTotal为0。在使用多种方法时,跟踪运行总数的最佳方法是什么?此外,我对代码中的任何批评或清理持开放态度。多谢各位Java快餐菜单(使用方法),java,methods,menu,Java,Methods,Menu,我正在写一个显示快餐菜单的程序。用户选择一个项目,然后输入该项目的数量,并可以继续选择具有特定数量的项目,直到完成。我不得不使用几种方法。我遇到的问题是计算一个运行总数。这是我的第一个Java类,所以我只知道基础知识。我将running total放在while循环中,这样它将继续向其中添加小计,但当我调用done()时,runningTotal为0。在使用多种方法时,跟踪运行总数的最佳方法是什么?此外,我对代码中的任何批评或清理持开放态度。多谢各位 import java.util.Scann
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 ($2.00) \n2. Fries ($1.50)\n3. Soda ($1.00) \n4. Done");
}
public static double itemPrice(int foodItem) {
if (foodItem == 1) {
// burger= $2.00
System.out.println("You've ordered a burger");
itemPrice = 2.00;
}
if (foodItem == 2) {
// fries = $1.50
System.out.println("You've ordered fries");
itemPrice = 1.50;
}
if (foodItem == 3) {
// soda = $1.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));
}
}
}
您正在重置
double runningTotal=0代码>在while循环中。另外,itemPrice
返回的价格需要添加到runningTotal
变量中
这就是您的主要方法的外观。用户完成后,不需要调用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);
}
输出:
Welcome
1. Burger ($2.00)
2. Fries ($1.50)
3. Soda ($1.00)
4. Done
1
You've ordered a burger
Enter quantity
2
Subtotal: 4.0
Welcome
1. Burger ($2.00)
2. Fries ($1.50)
3. Soda ($1.00)
4. Done
2
You've ordered fries
Enter quantity
1
Subtotal: 1.5
Welcome
1. Burger ($2.00)
2. Fries ($1.50)
3. Soda ($1.00)
4. Done
4
3.5
Enjoy your meal
Total amount: 3.5
每次迭代都将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 ($2.00) \n2. Fries ($1.50)\n3. Soda ($1.00) \n4. Done");
}
public static double itemPrice(int foodItem) {
if (foodItem == 1) {
//burger= $2.00
System.out.println("You've ordered a burger");
itemPrice = 2.00;
}
if (foodItem == 2) {
//fries = $1.50
System.out.println("You've ordered fries");
itemPrice = 1.50;
}
if (foodItem == 3) {
//soda = $1.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); {
}
}
}
这将带来精确的输出
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 ($2.00) \n2. Fries ($1.50)\n3. Soda
($1.00) \n4. Done");
}
public static double ItemPrice(int foodItem) {
if (foodItem == 1) {
// burger= $2.00
System.out.println("You've ordered a burger");
itemPrice = 2.00;
}
if (foodItem == 2) {
// fries = $1.50
System.out.println("You've ordered fries");
itemPrice = 1.50;
}
if (foodItem == 3) {
// soda = $1.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");
}
}
1.5
+1
+2
=3.5
?为用户提供有关问题的解释和解决方案将非常有用