简体   繁体   English

无法获得总金额

[英]Unable to get total amount

Hi guys I have fixed the initial problem but now its not adding up correctly. 嗨,大家好,我已经解决了最初的问题,但现在还不能正确解决。 I am unsure what to do and where I have gone wrong. 我不确定该怎么办以及哪里出了问题。 Any help would be appreciated. 任何帮助,将不胜感激。

import java.util.Scanner; 导入java.util.Scanner;

public class zoo { public static void main(String[] args) { 公共类动物园{公共静态void main(String [] args){

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

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

  int Option[] = new int[3];
  Option[0] = 1;
  Option[1] = 2;
  Option[2] = 3;

  boolean  continueLoop = true; 
  char resume;

    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;
              }

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

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

I have fixed issues and also updated code for better performance. 我已经解决了问题,还更新了代码以提高性能。

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);
        }
        }
    }
}

maybe you trying to do something like this: 也许您正在尝试执行以下操作:

public int someMethod(){
    int childTotal; // here variable must be initialized before return
                    // like this int childTotal = 0;
        switch (option) {
            case 1:
            childTotal=(int) ((double) quantity*childCost) ;
            System.out.println("Total amount for child tickets: $" + childTotal);
            break;
        }
        ...
        return childTotal;
}

in this case you'll get an error, that variable must be initialized. 在这种情况下,您会得到一个错误,该变量必须初始化。

Anyway you gave to few information about your problem. 无论如何,您几乎没有提供有关您的问题的信息。 Maybe you can show what kind of answer you get from the system, stackTrace or something like that. 也许您可以显示从系统,stackTrace或类似的东西得到什么样的答案。 Did you know about diference between initialize and declare? 您知道初始化和声明之间的区别吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM