gpt4 book ai didi

java - 无法获得应付总金额

转载 作者:行者123 更新时间:2023-12-02 10:58:27 25 4
gpt4 key购买 nike

problem

我设计了一个程序,当用户在询问是否要继续时输入“y”时,该程序将重新运行。我遇到的问题是,一旦用户输入“n”,程序应该显示购买的所有门票选项的应付总额。我已经花了几个星期的时间来解决这个问题,并且不确定下一步该做什么。我只包含了代码的底部部分。我还附上了一张照片来显示程序运行时出现的问题。

这是我的代码:

package cse1pgx_a2;
import java.util.Scanner;
public class CSE1PGX_A2 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {

int option, quantity, confirm;
float childTotal = 0;
float adultTotal = 0;
float seniorTotal = 0;
float finalTotal = 0;

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("\t"+ "@@@@@ Welcome to Zoos Victoria @@@@@");
System.out.println("\t" + "\t" + "MAIN MENU" + "\n");
System.out.println("\t" + "Zoo has the following ticketing options" + "\n");
System.out.println("\t" + "1 = Child (4-6 yrs)");
System.out.println("\t" + "2 = Adult (16+ yrs)");
System.out.println("\t" + "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 at " + childCost + " each!");

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 at " + adultCost + " each!");

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 at " + seniorCost + " each!");

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

break;
}

if (confirm !=1) {
System.out.println("Incorrect key!");
}

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

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:
finalTotal=(float) ((double) childTotal+adultTotal+seniorTotal) ;
System.out.println("Total amount payable: $ " + finalTotal);
break;

default:
System.out.println("Error");

}
}
}
}
}

最佳答案

我修复了问题并更新了代码以获得更好的性能。

package test;

import java.util.Scanner;

public class CSE1PGX_A2 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {

final float childCost = 18;
final float adultCost = 36;
final float seniorCost = 32.50F;

boolean continueLoop = true;
Scanner input = new Scanner(System.in);

float childTotal = 0;
float adultTotal = 0;
float seniorTotal = 0;

while (continueLoop) {
int option, confirm=0;

System.out.println("\t @@@@@ Welcome to Zoos Victoria @@@@@");
System.out.println("\t \t MAIN MENU \n");
System.out.println("\t Zoo has the following ticketing options \n");
System.out.println("\t 1 = Child (4-6 yrs)");
System.out.println("\t 2 = Adult (16+ yrs)");
System.out.println("\t 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:");
int quantity = input.nextInt();
childTotal = quantity * childCost;

System.out.println("You are purchasing " + quantity + " child tickets at " + childCost + " each!");
System.out.println("Press 1 to confirm");
confirm = input.nextInt();
if (confirm == 1) {
System.out.println("Total amount for child tickets: $" + childTotal);
}
break;
}
case 2: {
System.out.println("Enter total No of tickets for Adult:");
int quantity = input.nextInt();
adultTotal = quantity * adultCost ;

System.out.println("You are purchasing " + quantity + " adult tickets at " + adultCost + " each!");

System.out.println("Press 1 to confirm");
confirm = input.nextInt();
if (confirm == 1) {
System.out.println("Total amount for adult tickets $" + adultTotal);
}
break;
}
case 3: {
System.out.println("Enter total No of tickets for Senior:");
int quantity = input.nextInt();
seniorTotal = quantity * seniorCost ;
System.out.println("You are purchasing " + quantity + " senior tickets at " + seniorCost + " each!");

System.out.println("Press 1 to confirm");
confirm = input.nextInt();
if (confirm == 1) {
System.out.println("Total amount for senior tickets $" + seniorTotal);
}
break;
}
}

if (confirm != 1) {
System.out.println("Incorrect key!");
}

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

if (resume != 'y' && resume != 'Y') {
continueLoop = false;

System.out.println("Total amount for child tickets: $" + childTotal);
System.out.println("Total amount for senior tickets $" + seniorTotal);
System.out.println("Total amount for adult tickets $" + adultTotal);
float finalTotal = childTotal + adultTotal + seniorTotal ;
System.out.println("Total amount payable: $ " + finalTotal);
}
}
}
}

关于java - 无法获得应付总金额,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51536966/

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