Java 如何在菜单循环后执行print语句,然后返回主菜单?
在printMenuItems方法中,当用户选择要添加到购物车中的项目时,程序应该循环。当用户处于“项目”菜单中时,程序应在底部显示“您当前的总计为:$”,直到用户选择返回主菜单。回到主菜单后,程序底部应显示“您的总到期日为:$”。目前,在程序返回主菜单之前,我有一个“Your total due is:$”语句正在执行,当用户返回时,该语句将“Your total due is”语句置于主菜单之上。我正在想办法把这句话放在主菜单下面Java 如何在菜单循环后执行print语句,然后返回主菜单?,java,Java,在printMenuItems方法中,当用户选择要添加到购物车中的项目时,程序应该循环。当用户处于“项目”菜单中时,程序应在底部显示“您当前的总计为:$”,直到用户选择返回主菜单。回到主菜单后,程序底部应显示“您的总到期日为:$”。目前,在程序返回主菜单之前,我有一个“Your total due is:$”语句正在执行,当用户返回时,该语句将“Your total due is”语句置于主菜单之上。我正在想办法把这句话放在主菜单下面 import java.util.Scanner; publ
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;
}
}
}
}
import java.util.Scanner;
公共课家庭作业1{
静态双用户合计=0;
静态双用户信用=100;
静态双[]菜单={19.99,29.49,15.79,24.99,24.99};
静态扫描仪myScanner=新扫描仪(System.in);
公共静态int printmain菜单(){
System.out.println(“请从下面的列表中选择一个菜单项:\n”+
“1-查看项目菜单\n”+
“2-应付款项总额\n”+
“3-在信用卡中添加$5\n”+
“4-明确命令”);
int userChoice=myScanner.nextInt();
if(userChoice>0&&userChoice<5){
返回用户选择;
}
否则{
System.out.println(“请输入有效选项!”);
printmain菜单();
}
返回0;
}
公共静态双打印菜单项(double userTotal){
System.out.println(“您希望向订单中添加什么?\n”+
“(1)烤面包机:$19.99\n”+
“(2)咖啡机:24.49美元\n”+
“(3)华夫饼制造商:$15.79\n”+
“(4)搅拌机:$24.99\n”+
(5)水壶:24.99美元\n+
“(6)进入主菜单”);
System.out.println(“您当前的总数为:$”+userTotal);
int menuSelections=myScanner.nextInt();
如果(菜单选择==1){
System.out.println(“您在订单中添加了一个烤面包机。”);
userTotal+=菜单[0];
}
else if(menuSelections==2){
System.out.println(“您在订单中添加了一个咖啡壶。”);
userTotal+=菜单[1];
}
else if(菜单选择==3){
System.out.println(“您在订单中添加了一个华夫饼干制造商。”);
userTotal+=menuPrices[2];
}
else if(菜单选择==4){
System.out.println(“您在订单中添加了一个搅拌机。”);
userTotal+=菜单[3];
}
else if(menuSelections==5){
System.out.println(“您在订单中添加了一个水壶。”);
userTotal+=菜单[4];
}
else if(菜单选择==6){
System.out.println(“您的到期总金额为:$”+userTotal);
返回userTotal;
}
否则{
System.out.println(“无效项目编号,请重试”);
}
返回printMenuItems(用户总数);
}
公共静态布尔payTotalDue(double userTotal){
double preTaxTotal=用户总数;
如果(税前合计>50){
preTaxTotal-=(preTaxTotal*0.2);
preTaxTotal+=(preTaxTotal*0.085);
如果(preTaxTotal仅在集团下
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");
加
并从打印组中删除
else if (menuSelections == 6) {
System.out.println("Your total due is: $" + userTotal);
return userTotal;
}
另外,使用java开关比使用其他许多开关更好/更简单
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;
}
因此,显然我需要“您的总到期日为:$”语句来显示,即使总到期日为$0.00。但是,当程序第一次启动并显示主菜单时,它不可能在那里。您知道我如何处理此问题吗?我更新了上面的答复,以包括runOnce代码块。也许这就是您正在寻找的。
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;
}