Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java订货系统中如何计算总价?_Java - Fatal编程技术网

Java订货系统中如何计算总价?

Java订货系统中如何计算总价?,java,Java,我只是一名大学一年级的学生,我的项目是创建一个订购系统,但我在定价上卡住了,因为我不能正确地计算总价。当程序循环时,它计算的价格是错误的。A1和A2是我唯一的选择,因为这仍然是一个未完成的项目 import java.io.*; public class Ordering_System { public static void main(String []args) throws Exception { BufferedReader br=new Buffe

我只是一名大学一年级的学生,我的项目是创建一个订购系统,但我在定价上卡住了,因为我不能正确地计算总价。当程序循环时,它计算的价格是错误的。A1和A2是我唯一的选择,因为这仍然是一个未完成的项目

import java.io.*;
public class Ordering_System
{
     public static void main(String []args) throws Exception
     {
         BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
         String order,again;
         int quantity,price1=0,price2=0,loop1=0,quantity1,quantity2=0;

         System.out.println("  ");  

         System.out.println("Welcome to MgRonalds, What do you want to order?");

         System.out.println(" ");
         System.out.println("*******************************************************************");
         System.out.println("*   Code    Meal             ''MENU''              Price          *");
         System.out.println("*                                                                 *");
         System.out.println("*   (A1)   MgBurger                                P30.00         *");
         System.out.println("*   (A2)   Big Mac                                 P139.00        *");
         System.out.println("*   (B1)   Cheese Burger                           P35.00         *");
         System.out.println("*   (B2)   Chicken Burger                          P50.00         *");
         System.out.println("*   (C1)   MgNuggets                               P65.00         *");
         System.out.println("*   (C2)   MgChicken                               P79.00         *");
         System.out.println("*   (D1)   MgSpagetti                              P60.00         *");
         System.out.println("*   (D2)   MgFries                                 P40.00         *");
         System.out.println("*   (E1)   Coke                                    P10.00         *");
         System.out.println("*   (E2)   Sprite                                  P10.00         *");
         System.out.println("*   (E3)   Royal                                   P10.00         *");
         System.out.println("*   (F1)   Sundae                                  P25.00         *");
         System.out.println("*   (F2)   MgFloat                                 P25.00         *");
         System.out.println("*                                                                 *");
         System.out.println("*******************************************************************");

         do{
            System.out.println("");
            System.out.print("Enter Code Order      : ");
            order=br.readLine();
            if (order.equalsIgnoreCase("A1")) {
                price1=30;
                System.out.println("Order Description     : MgBurger ");
            } else if (order.equalsIgnoreCase("A2")) {
                price1=139;
                System.out.println("Order Description     : Big Mac ");
            }   

            System.out.print("Enter Quantity        : ");
            quantity1= Integer.parseInt(br.readLine());
            quantity2=quantity1+quantity2;

            price2=price1*quantity2;   

            System.out.print("Another Order?  (Y/N) : ");
            again=br.readLine();
            if (again.equalsIgnoreCase("y"))
                loop1=loop1+1;
            else loop1=loop1-100;
      } while (loop1==1);    

     System.out.println(" ");
     System.out.println("Total Price           : "+price2);   

 }
}
以下是一个示例输出:

Enter Code Order      : a1
Order Description     : MgBurger 
Enter Quantity        : 2
Another Order?  (Y/N) : y

Enter Code Order      : a2
Order Description     : Big Mac 
Enter Quantity        : 2
Another Order?  (Y/N) : n

Total Price           : 556

答案应该是338而不是556让我们逻辑地解决这个问题:

第一个循环中:

输入代码顺序:a1

现在,
price1=30

订单描述:MgBurger

输入数量:2

同意吗

再来一份?(是/否):是

在第二个循环中,我们仍然有
price1=30
quantity1=2
quantity2=2
price2=60
。现在:

输入代码顺序:a2

订单描述:巨无霸

输入数量:2


发现错误了吗
quantity2
仍保留循环中上一个事务的值。

您只需这样做如何? 添加一个变量
total=0

  System.out.print("Enter Quantity        : ");
  quantity1 = Integer.parseInt(br.readLine());
  // quantity2=quantity1+quantity2;

  // price2=price1*quantity2;

  total += price1 * quantity1;

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

您可以将代码简化如下。也可以考虑使用浮动值来代替价格INT/P>
public static void main(String[] args) throws Exception {
  System.out.println("  ");

  // Display the Menu

  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  String order, again;
  int quantity = 0;
  int total = 0;
  while (true) {
    System.out.println("");
    System.out.print("Enter Code Order      : ");
    order = br.readLine();
    System.out.print("Enter Quantity        : ");
    quantity = Integer.parseInt(br.readLine());

    total += getPrice(order) * quantity;

    System.out.print("Another Order?  (Y/N) : ");
    again = br.readLine();
    if (again.equalsIgnoreCase("N")) {
      break;
    }
  }

  System.out.println(" ");
  System.out.println("Total Price           : " + total);
}

private static int getPrice(String order) {
  int price = 0;
  // using switch-case would be more appropriate
  if (order.equalsIgnoreCase("A1")) {
    price = 30;
    System.out.println("Order Description     : MgBurger ");
  } else if (order.equalsIgnoreCase("A2")) {
    price = 139;
    System.out.println("Order Description     : Big Mac ");
  }
  return price;
}

我建议您在调试器中单步执行代码,并在代码执行期间观察值。我打赌你会很快发现你在哪里根据错误的值计算东西:)而且,如果你给你的变量提供更具描述性的名称,比如
totalPrice
priceForCurrentItem
等,也会有所帮助,当您在错误的位置使用变量时,这将更加清楚。我从来不知道
McDonalds
有一个名为
MgRonalds
的新特许经营权:谢谢你!!你真的帮了我这个忙。从昨天起这就是我的问题。。
quantity1 = 2;
quantity2 = quantity1 + quantity2 = 2 + 2 = 4;
price2 = 139 * 4 = 556;
  System.out.print("Enter Quantity        : ");
  quantity1 = Integer.parseInt(br.readLine());
  // quantity2=quantity1+quantity2;

  // price2=price1*quantity2;

  total += price1 * quantity1;

  ...
  System.out.println(" ");
  System.out.println("Total Price           : " + total);
public static void main(String[] args) throws Exception {
  System.out.println("  ");

  // Display the Menu

  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  String order, again;
  int quantity = 0;
  int total = 0;
  while (true) {
    System.out.println("");
    System.out.print("Enter Code Order      : ");
    order = br.readLine();
    System.out.print("Enter Quantity        : ");
    quantity = Integer.parseInt(br.readLine());

    total += getPrice(order) * quantity;

    System.out.print("Another Order?  (Y/N) : ");
    again = br.readLine();
    if (again.equalsIgnoreCase("N")) {
      break;
    }
  }

  System.out.println(" ");
  System.out.println("Total Price           : " + total);
}

private static int getPrice(String order) {
  int price = 0;
  // using switch-case would be more appropriate
  if (order.equalsIgnoreCase("A1")) {
    price = 30;
    System.out.println("Order Description     : MgBurger ");
  } else if (order.equalsIgnoreCase("A2")) {
    price = 139;
    System.out.println("Order Description     : Big Mac ");
  }
  return price;
}