当前位置: 首页 > 知识库问答 >
问题:

无法获取应付款总额[重复]

蓟清野
2023-03-14

我设计了一个程序,当用户输入“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个答案

徐凌
2023-03-14

我想玩,但不完全理解问题…所以我从头开始开发应用程序。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);
        }
    }
}

阅读方面的进步。我会首先检查我读到的是否是字符。。

拓拔霄
2023-03-14

试试这个代码。我希望这有帮助。

   public static void main(String[] args) {
          int option, quantity, confirm;       //minor change

          float childTotal = 0;
          float adultTotal = 0;
          float seniorTotal = 0;
          float finalTotal = 0;            //minor change

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

          boolean  continueLoop = true; 
          char resume;
          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+=(double) quantity*childTotal ; //minor change
                            System.out.println("Total amount payable: $" + childTotal);
                            break;
                        case 2:
                            finalTotal+=(double)  quantity*adultTotal ; //minor change
                            System.out.println("Total amount payable $" + adultTotal);
                            break;
                        default:
                            finalTotal+=(double) quantity*seniorTotal; //minor change
                            System.out.println("Total amount payable $" + seniorTotal);
                            break;
                      }
           }
    }
        }
    }
卞安邦
2023-03-14

我已经修复了问题,也更新了代码以获得更好的性能。

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);
        }
        }
    }
}
 类似资料:
  • 嗨,伙计们,我已经修复了最初的问题,但现在它不是正确的加起来。我不确定该做什么,哪里出了问题。如有任何帮助,我们将不胜感激。 导入java.util.scanner; public类zoo{public static void main(String[]args){

  • 贝宝快递结账API文档规定,“买家的交易总成本”(PAYMENTREQUEST_n_AMT)不能超过10,000美元。看到这里... https://developer.paypal.com/docs/classic/api/merchant/SetExpressCheckout_API_Operation_NVP 我希望为一个奢侈品电子商务网站实施快速结账,其中许多项目超过10,000美元。 是

  • 我正在为一个在线购物网站实施stripe,该网站在“余额”系统上运行,用户在该系统中输入他们想在交易余额中存入的金额,并提交信用卡信息进行存款。 我不知道如何避免在付款确认时进行两次连续的api调用stripe:一次使用html表单中的存款当前值更新付款金额,一次确认付款意图。 以下是我理解的限制条件: Stripe需要创建一个支付意图,以便用他们的信用卡表单填充iframe 为了简化存款体验,我

  • 我有一个java的家庭作业。我的任务是建立一个可以取款、存款和查询余额的银行。我的问题是在存款和取款后我无法更新我的余额...我已经尝试了我能做的一切,但仍然无法得到逻辑。有人可以帮助添加到我的程序中吗...谢谢 ---------------------------------主要的--------------------------------

  • 我们是一家小型初创企业,大约两个月前开始。我们使用Stripe作为主要支付处理器,同时使用PayPal作为次要选项。 我们的计划是每月订阅,每月从$ 5到$ 25不等。我们的付款表单当前收集用户的姓名,CC,到期日期和CVC安全代码。 第一个月后,我们开始注意到用户的定期付款在 Stripe 上开始失败。我们不知道为什么会发生这种情况。如果第一笔付款成功,为什么未来的订阅付款会失败?到目前为止,我

  • 我知道,要放入值,我可以,要检索值,我可以,但在下面的代码中,它的行为不像预期的那样。检索值时,我获得。 设置值的代码: 并且我正在检索BroadCaseReceiver的onReceive中的值:[省略的无关代码]