无法获得应付总额

时间:2018-07-26 10:45:26

标签: java netbeans

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

                  }
       }
}
    }
}

3 个答案:

答案 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)

尝试此代码。希望对您有所帮助。

request.getSession().invalidate();

答案 2 :(得分:0)

我想玩游戏,但不完全理解问题...因此我从头开始开发了该应用程序。 Suryakant速度更快,因此请接受他的回答(如果它可以解决您的问题)。我只是将其发布在这里,因为我已经完成了:-)

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Test {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        boolean continueLoop = true;
        Map<TicketType, Integer> purchases=new HashMap<>();
        do {
            TicketType type = printMenu(scan);
            System.out.println("Enter number of tickets for " + type.label);
            int quantity = scan.nextInt();
            System.out.println("You are purchasing "+quantity + " "+ type.label+ " ticket at "+type.cost+" each. " +
                    "Press 1 to confirm?");
            int confirm= scan.nextInt();
            if (confirm!=1) continue;
            if (purchases.containsKey(type)){
                purchases.put(type,purchases.get(type)+quantity);
                System.out.println("You now have " +purchases.get(type) +" "+type.label +" tickets in total");
            }else {
                purchases.put(type,quantity);
            }
            System.out.println("You have added " +quantity +" "+type.label +" tickets in your basket.");
            System.out.println("Do you wish to continue (Y|N)?");
            String resume=scan.next();
            if (resume.startsWith("Y") || resume.startsWith("y")){
                continueLoop=true;
            }else {
                continueLoop=false;
            }
        }while (continueLoop);

        System.out.println("Purchases");
        long total=0;
        for (Map.Entry<TicketType, Integer> ticketTypeIntegerEntry : purchases.entrySet()) {
            System.out.println(ticketTypeIntegerEntry.getKey().label+"("+ticketTypeIntegerEntry.getValue()+")");
            total+=ticketTypeIntegerEntry.getKey().cost*ticketTypeIntegerEntry.getValue();
        }

        System.out.println("Total payable ammount: "+total);

    }

    private static TicketType printMenu(Scanner scan) {
        System.out.println("Welcome");
        TicketType type;
        int k = -1;
        do {
            for (TicketType ticketType : TicketType.values()) {
                System.out.println(ticketType.id + ". for " + ticketType.label);
            }
            System.out.println("Enter your option");
            k = scan.nextInt();
        } while ((type=TicketType.valuefromId(k))==null);
        return type;
    }

    private enum TicketType {
        CHILD(1, "Child", 18D),
        ADULT(2, "Adult", 36D),
        SENIOR(3, "Senior", 18.5D);
        int id;
        String label;
        double cost;

        private static Map<Integer,TicketType> map=new HashMap<Integer,TicketType>();
        static {
            for (TicketType ticketType : TicketType.values()) {
                map.put(ticketType.id,ticketType);
            }
        }
        TicketType(int id, String label, double cost) {
            this.id = id;
            this.label = label;
            this.cost=cost;
        }

        public static TicketType valuefromId(int id){
                return map.get(id);
        }
    }
}

阅读中有进步。.我会先检查我阅读的内容是否是字符。