无法获得应付总额 [英] Unable to get total amount payable

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

问题描述

我设计了一个程序,当用户询问是否要继续时,该程序将在用户输入"y"时重新运行.我遇到的问题是,一旦用户输入"n",程序就应该显示从所有已购买的票务选项中应支付的总金额.我花了几个星期解决这个问题,不确定下一步该怎么做.我只包括了代码的底部.我还附有一张照片,以显示程序运行时出现的问题.

I have designed a program which will rerun when the user enters "y" when asked if they wish to continue. The problem I am having is once the user enters "n" the program is supposed to display the total amount payable from all ticket options purchased. I have spent a couple of weeks stuck on this problem and am unsure of what to do next. I have only included the bottom part of my code. I have also included a photo to show my problem when the program is run.

这是我的代码:

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

                  }
       }
}
    }
}

推荐答案

我已经解决了一些问题,并更新了代码以提高性能.

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天全站免登陆