gpt4 book ai didi

java - 如何在菜单循环后执行打印语句然后返回主菜单?

转载 作者:行者123 更新时间:2023-12-01 18:23:07 25 4
gpt4 key购买 nike

在 printMenuItems 方法中,程序应在用户选择要添加到购物车中的项目时循环。当用户位于项目菜单中时,程序应在底部显示“您当前的总计为:$”,直到用户选择返回主菜单。返回主菜单后,程序底部应显示“您的应付款总额为:$”。我目前在程序返回主菜单之前执行“您的总应付款为:$”语句,当用户返回时,该语句将“您的总应付款为”语句置于主菜单上方。我正在尝试弄清楚如何将该语句放在主菜单下方

import java.util.Scanner;
public class Homework1 {

static double userTotal = 0;
static double userCredit = 100;
static double [] menuPrices = {19.99, 29.49, 15.79, 24.99, 24.99};
static Scanner myScanner = new Scanner(System.in);

public static int printMainMenu() {
System.out.println("Please select a menu item from the list below: \n" +
"1 - View item menu \n" +
"2 - Pay total due \n" +
"3 - Add $5 in credit \n" +
"4 - Clear order");

int userChoice = myScanner.nextInt();
if (userChoice > 0 && userChoice < 5) {
return userChoice;
}
else {
System.out.println("Please enter a valid option!");
printMainMenu();
}
return 0;
}

public static double printMenuItems(double userTotal) {
System.out.println("What would you like to add to your order? \n" +
"(1) Toaster: $19.99 \n" +
"(2) Coffee maker: $24.49 \n" +
"(3) Waffle maker: $15.79 \n" +
"(4) Blender: $24.99 \n" +
"(5) Kettle: $24.99 \n" +
"(6) Go to the main menu");
System.out.println("Your current total is: $" + userTotal);
int menuSelections = myScanner.nextInt();

if (menuSelections == 1) {
System.out.println("You have added a Toaster to your order.");
userTotal += menuPrices[0];
}
else if (menuSelections == 2) {
System.out.println("You have added a Coffee maker to your order.");
userTotal += menuPrices[1];
}
else if (menuSelections == 3) {
System.out.println("You have added a Waffle maker to your order.");
userTotal += menuPrices[2];
}
else if (menuSelections == 4) {
System.out.println("You have added a Blender to your order.");
userTotal += menuPrices[3];
}
else if (menuSelections == 5) {
System.out.println("You have added a Kettle to your order.");
userTotal += menuPrices[4];
}
else if (menuSelections == 6) {
System.out.println("Your total due is: $" + userTotal);
return userTotal;
}
else {
System.out.println("Invalid item number please try again");
}
return printMenuItems(userTotal);

}

public static boolean payTotalDue(double userTotal) {
double preTaxTotal = userTotal;
if (preTaxTotal > 50) {
preTaxTotal -= (preTaxTotal * 0.2);
preTaxTotal += (preTaxTotal * 0.085);

if (preTaxTotal <= userCredit) {
System.out.println("Thank you! You saved: $" + (userTotal + (userTotal * 0.085) - preTaxTotal) +
" Your change is: $" + (userCredit - preTaxTotal));
System.out.println("Your items will be on their way soon!");

}
else {
System.out.println("Insufficient funds!");
System.exit(0);
}

}
else {
preTaxTotal += (preTaxTotal * 0.085);
System.out.println("Thank you! Your change is: $" + (userCredit - preTaxTotal));
System.out.println("Your items will be on their way soon!");
}
return true;
}

public static void main(String[] args) {

while (true) {
int mainMenuSelection = printMainMenu();
if (mainMenuSelection == 1) {
userTotal = printMenuItems(userTotal);

}
else if (mainMenuSelection == 2) {
if (payTotalDue(userTotal)) {
userTotal = 0;
break;
}
}
else if (mainMenuSelection == 3) {
userCredit += 5;
System.out.println("Credit available: $" + userCredit);
}
else if (mainMenuSelection == 4) {
System.out.println("Current order balance has been cleared. Current due: $0.00");
userTotal = 0;
}
}
}
}

最佳答案

简单地在集团之下

 public static int printMainMenu() {
System.out.println("Please select a menu item from the list below: \n" +
"1 - View item menu \n" +
"2 - Pay total due \n" +
"3 - Add $5 in credit \n" +
"4 - Clear order");

添加

if ( userTotal > 0 ) {
System.out.println("Your total due is: $" + userTotal);
}

并从 block 中删除打印

 else if (menuSelections == 6) {
System.out.println("Your total due is: $" + userTotal);
return userTotal;
}

此外,使用 java switch 而不是许多 if else 会更好/更简单

 switch (myScanner.nextInt()) {
case 1:
System.out.println("You have added a Toaster to your order.");
userTotal += menuPrices[0];
break;
case 2:
System.out.println("You have added a Coffee to your order.");
userTotal += menuPrices[0];
break;
case 3: (continue with same..)
default:
System.out.println("Invalid item number please try again");
}

根据问题,一旦您可以使用这个简单的代码片段,我如何运行代码打印输出

static boolean doOnce = true; //put this somewhere to be executed once , like begin of your program
...
if ( doOnce ) {
System.out.println("Your total due is: $" + userTotal);
doOnce = false;
}

关于java - 如何在菜单循环后执行打印语句然后返回主菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60271774/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com