无法获得总金额

时间:2018-06-29 10:21:44

标签: java netbeans

大家好,我已经解决了最初的问题,但现在还不能正确解决。我不确定该怎么办以及哪里出了问题。任何帮助将不胜感激。

导入java.util.Scanner;

公立动物园{     公共静态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;
          }

2 个答案:

答案 0 :(得分:1)

我已解决问题,并更新了代码以提高性能。

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

答案 1 :(得分:0)

也许您正在尝试执行以下操作:

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

在这种情况下,您会得到一个错误,该变量必须初始化。

无论如何,您很少提供有关您的问题的信息。也许您可以显示从系统,stackTrace或类似的东西得到什么样的答案。 您知道初始化和声明之间的区别吗?