gpt4 book ai didi

java - 如果选择"is",则用户返回主菜单

转载 作者:行者123 更新时间:2023-12-02 11:05:12 24 4
gpt4 key购买 nike

大家好,我对如何执行此操作有疑问,我已经用谷歌搜索了它,但它没有多大意义。我需要这样做;该程序询问用户是否希望继续。 如果选择是,将返回主菜单。 如果选择“否”,则应付总额将为显示然后程序将终止

  int option, quantity, confirm;
float childTotal;
float adultTotal;
float seniorTotal;

final double childCost = 18;
final double adultCost = 36;
final double seniorCost = 32.50;

char resume;

Scanner input = new Scanner(System.in);

System.out.println("1 = Child (4-6 yrs)");
System.out.println("2 = Adult (16+ yrs)");
System.out.println("3 = Senior (60+ yrs)" + "\n");

System.out.println("Enter your option:" );
option=input.nextInt();

switch (option) {
case 1:
System.out.println("Enter total No of tickets for Child:" );
quantity=input.nextInt();

System.out.println("You are purchasing " + quantity + " child tickets");

System.out.println("Press 1 to confirm");
confirm=input.nextInt();

break;

case 2:
System.out.println("Enter total No of tickets for Adult:" );
quantity=input.nextInt();

System.out.println("You are purchasing " + quantity + " adult tickets");

System.out.println("Press 1 to confirm");
confirm=input.nextInt();

break;

default:
System.out.println("Enter total No of tickets for Senior:" );
quantity=input.nextInt();

System.out.println("You are purchasing " + quantity + " senior tickets");

System.out.println("Press 1 to confirm");
confirm=input.nextInt();

break;
}

if (confirm !=1) {
System.out.println("Incorrect key! User to go back to main menu");
}


System.out.println("Do you wish to continue? (Y/N) ");
resume = input.next().charAt(0);

if (resume == 'y' || resume == 'Y') {

} else {
switch (option) {
case 1:
childTotal=(int) ((double) quantity*childCost) ;
System.out.println("Total amount for child tickets: $" + childTotal);
break;
case 2:
adultTotal=(int) ((double) quantity*adultCost) ;
System.out.println("Total amount for adult tickets $" + adultTotal);
break;
default:
seniorTotal=(int) ((double) quantity*seniorCost);
System.out.println("Total amount for senior tickets $" + seniorTotal);
break;
}
}

最佳答案

创建一个设置为 true 的 boolean 变量。

boolean continueLoop = true;

将主要逻辑添加到 while 循环中,直到 continue 为 true

while(continueLoop){
//Do your code here
System.out.println("Do you wish to continue? (Y/N) ");
resume = input.next().charAt(0);
if (resume == 'y' || resume == 'Y'){}
else{
//Do Code here
continueLoop=false;
}
} //End while loop.

在 while 循环之后继续您的代码。我已将resume == y 的条件更改为resume !=y,因为如果用户不按y,代码应该停止迭代。

你的代码将变成

int option, quantity, confirm;
float childTotal;
float adultTotal;
float seniorTotal;

final double childCost = 18;
final double adultCost = 36;
final double seniorCost = 32.50;

boolean continueLoop = true;
char resume;

Scanner input = new Scanner(System.in);
while(continueLoop){
System.out.println("1 = Child (4-6 yrs)");
System.out.println("2 = Adult (16+ yrs)");
System.out.println("3 = Senior (60+ yrs)" + "\n");

System.out.println("Enter your option:" );
option=input.nextInt();

switch (option) {
case 1:
System.out.println("Enter total No of tickets for Child:" );
quantity=input.nextInt();

System.out.println("You are purchasing " + quantity + " child tickets");

System.out.println("Press 1 to confirm");
confirm=input.nextInt();

break;

case 2:
System.out.println("Enter total No of tickets for Adult:" );
quantity=input.nextInt();

System.out.println("You are purchasing " + quantity + " adult tickets");

System.out.println("Press 1 to confirm");
confirm=input.nextInt();

break;

default:
System.out.println("Enter total No of tickets for Senior:" );
quantity=input.nextInt();

System.out.println("You are purchasing " + quantity + " senior tickets");

System.out.println("Press 1 to confirm");
confirm=input.nextInt();

break;
}

if (confirm !=1) {
System.out.println("Incorrect key! User to go back to main menu");
}


System.out.println("Do you wish to continue? (Y/N) ");
resume = input.next().charAt(0);

if (resume == 'y' || resume == 'Y') {
}else{
continueLoop = false;
switch (option) {
case 1:
childTotal=(int) ((double) quantity*childCost) ;
System.out.println("Total amount for child tickets: $" + childTotal);
break;
case 2:
adultTotal=(int) ((double) quantity*adultCost) ;
System.out.println("Total amount for adult tickets $" + adultTotal);
break;
default:
seniorTotal=(int) ((double) quantity*seniorCost);
System.out.println("Total amount for senior tickets $" + seniorTotal);
break;
}
}
}
}

关于java - 如果选择"is",则用户返回主菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51022792/

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