无法获得总金额 [英] Unable to get total amount

查看:107
本文介绍了无法获得总金额的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我已经解决了最初的问题,但现在它没有正确添加。我不确定该做什么以及我哪里出错了。任何帮助将不胜感激。

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;

import java.util.Scanner;

public class zoo {
public static void main(String [] args){

public class zoo { public static 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);
        }
        }
    }
}

这篇关于无法获得总金额的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆