【问题标题】:JAVA How to change variable value to zero each time it loops backJAVA每次循环返回时如何将变量值更改为零
【发布时间】:2016-04-06 09:42:59
【问题描述】:

我使用 for 循环要求用户输入要购买的商品并计算价格...然后它会询问用户是否会继续添加另一个商品...所以它又是一个循环...然后当用户停止购买...然后它会计算总价...所以我使用:

    totalprice+=total;

但是当外部 for 循环将为第二个客户重复时...totalprice 的价值仍然是第一个客户的购买价值,所以它会加起来...无论如何我可以每次都将 totalprice 值恢复为 0它为第二个客户循环?

这是我的那个方法的代码

public static void makeOrder() {
    for (index = 0; index < date.length; index++) {
        double price = 0;
        int order;
        char addOrder;
        String resume;
        System.out.print("\nCustomer" + (index + 1));
        System.out.print("\nEnter your name: ");
        String name = input.nextLine();
        System.out.print("Enter the date of reservation(DD/MM/YY): ");
        date[index] = input.nextLine();
        System.out.print("Enter your type of table(Couple/Family): ");
        String table = input.nextLine();
        do {
            System.out.println("BERKAT RESTAURANT MENU:\n\n MEALS \n1-Beef      Bolognese: RM17.00\n2-Chicken Marsala: RM 13.00\n3-Spaghetti Carbonara: RM  9.00\n4-Fillet Mignon: RM12.00");
            System.out.println("\nDRINKS \n5-Strawberry Fruit Punch: RM6.00 \n6-Vanilla Smoothies: RM 7.00\n7-Sky Juice: RM 3.00");
            System.out.print("\nEnter your choice of meals/drink: ");
            order = input.nextInt();
            System.out.print("Enter the quantity: ");
            int quantity = input.nextInt();
            System.out.print("Do you want to add order?(Y/N): ");
            addOrder = input.next().charAt(0);

            double total = calculatePrice(order, quantity);

            subtotal += total;

        } while (addOrder != 'N');

        System.out.println("");

        System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
        System.out.printf("The total price you have to pay is: RM%6.2f ", subtotal);
        System.out.println("");
        System.out.println("Thank you for coming to our restaurant, Please come again!");
        System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");

    }
}// makeOrder method

每次“for”循环返回时,我都想将小计值恢复为 0.00

【问题讨论】:

  • 查看更多代码会有所帮助,以便我们了解您需要什么帮助。
  • 你的意思是像totalprice = 0;
  • 请附上您的代码,因为它使社区更容易提供帮助。
  • 对此感到抱歉...我将问题重新编辑...
  • 小计在哪里声明?您在这里处理范围规则,因此如果没有声明的位置,我们无法明确回答。

标签: java arrays for-loop repeat


【解决方案1】:

您要确保在外部 for 循环开始时将 subtotal 初始化为 0。这样对于每个客户,它始终为 0,无论添加到订单中的商品数量如何。

public static void makeOrder() {
  double subtotal = 0;
  for(index = 0, index < date.length; index++) {
    subtotal = 0;
    /* your other code here */
  }
}

【讨论】:

    【解决方案2】:

    您应该在 for 循环开始时将 subtotal 初始化为 0。

    public static void makeOrder()
    {
      for(index=0;index<date.length;index++)
      {
        double subtotal=0;
        double price=0;
        int order;
        char addOrder;
        String resume;
        System.out.print("\nCustomer"+(index+1));
        System.out.print("\nEnter your name: ");
        String name=input.nextLine();
        System.out.print("Enter the date of reservation(DD/MM/YY): ");
        date[index]=input.nextLine();
        System.out.print("Enter your type of table(Couple/Family): ");
        String table=input.nextLine();
        do{
          System.out.println("BERKAT RESTAURANT MENU:\n\n MEALS \n1-Beef      Bolognese: RM17.00\n2-Chicken Marsala: RM 13.00\n3-Spaghetti Carbonara: RM  9.00\n4-Fillet Mignon: RM12.00");
          System.out.println("\nDRINKS \n5-Strawberry Fruit Punch: RM6.00 \n6-Vanilla Smoothies: RM 7.00\n7-Sky Juice: RM 3.00");
          System.out.print("\nEnter your choice of meals/drink: ");
          order=input.nextInt();
          System.out.print("Enter the quantity: ");
          int quantity=input.nextInt();
          System.out.print("Do you want to add order?(Y/N): ");
          addOrder=input.next().charAt(0);
    
          double total=calculatePrice(order,quantity);
    
          subtotal+=total;
    
        }while(addOrder!='N');
    
        System.out.println("");
        System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
        System.out.printf("The total price you have to pay is: RM%6.2f ",subtotal);
        System.out.println("");
        System.out.println("Thank you for coming to our restaurant, Please come again!");
        System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
    
      }
    }//makeOrder method
    

    【讨论】:

      【解决方案3】:

      谢谢你们的帮助...所以我声明 subtotal=0.00 它就像一个魅力

         public static void makeOrder()
        {
        for(index=0;index<date.length;index++)
        {
        double price=0;
        double subtotal=0.00;
        int order;
        char addOrder;
        String resume;
           System.out.print("\nCustomer"+(index+1));
           System.out.print("\nEnter your name: ");
           String name=input.nextLine();
           System.out.print("Enter the date of reservation(DD/MM/YY): ");
           date[index]=input.nextLine();
           System.out.print("Enter your type of table(Couple/Family): ");
           String table=input.nextLine();
           do{
           System.out.println("BERKAT RESTAURANT MENU:\n\n MEALS \n1-Beef Bolognese: RM17.00\n2-Chicken Marsala: RM 13.00\n3-Spaghetti Carbonara: RM 9.00\n4-Fillet Mignon: RM12.00");
           System.out.println("\nDRINKS \n5-Strawberry Fruit Punch: RM6.00 \n6-Vanilla Smoothies: RM 7.00\n7-Sky Juice: RM 3.00");
           System.out.print("\nEnter your choice of meals/drink: ");
           order=input.nextInt();
           System.out.print("Enter the quantity: ");
           int quantity=input.nextInt();
           System.out.print("Do you want to add order?(Y/N): ");
           addOrder=input.next().charAt(0);
      
           double total=calculatePrice(order,quantity);
      
           subtotal+=total;
      
           }while(addOrder!='N');
      
           System.out.println("");
      
        System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
        System.out.printf("The total price you have to pay is: RM%6.2f ",subtotal);
        System.out.println("");
        System.out.println("Thank you for coming to our restaurant, Please come again!");
        System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
      
      
           }
           }//makeOrder method
      

      【讨论】:

        猜你喜欢
        • 2013-05-15
        • 1970-01-01
        • 2013-02-06
        • 2021-11-30
        • 1970-01-01
        • 1970-01-01
        • 2016-01-27
        • 2021-11-25
        • 2020-01-13
        相关资源
        最近更新 更多