Java 比萨饼游戏中的计算错误

Java 比萨饼游戏中的计算错误,java,Java,我要做一个披萨游戏。。。我已经写了所有的代码。。大多数情况下它似乎都能正常工作。问题是我的数字并不像他们想象的那样。。。我知道我的错误在我计算奶酪皮的时候。。。。有人能帮忙吗 如果顾客选择添加奶酪皮(除了薄脆以外的任何东西),那么在比萨饼的基本价格中添加以下内容: k = ec where: k = total cost of cheesy crust (dollars) e = size of pizza crust (inches) c = cost of mate

我要做一个披萨游戏。。。我已经写了所有的代码。。大多数情况下它似乎都能正常工作。问题是我的数字并不像他们想象的那样。。。我知道我的错误在我计算奶酪皮的时候。。。。有人能帮忙吗

如果顾客选择添加奶酪皮(除了薄脆以外的任何东西),那么在比萨饼的基本价格中添加以下内容:

k = ec

where:
    k = total cost of cheesy crust (dollars)
    e = size of pizza crust (inches)
    c = cost of materials (dollars per square inch - 0.02)
比萨饼皮的大小是衡量比萨饼边界的尺度:

for round pizzas:   e =  2π(d/2)    (the circumference)
for rectangular pizzas: e =  2(L+w) (the perimeter)

where
    e = size of pizza crust (inches)
    d = diameter of round pizza (inches)
    L = length of rectangular pizza (inches)
    w = width of rectangular pizza (inches)






enter code here
import java.util.Scanner;

public class Pizza {

    /*
     * calculates price including tax
     * @return double
     */
   public static double calculatePriceWithTax(double price){

      return price * (1 + 0.07);
   }
    /*
     * calculates delivery fee if any
     * @return double
     */
   public static double deliveryFee(double price){
      if(price < 10){
         return 3;
      }

      else if(price >= 10 && price <= 20){
         return 2;
      }

      else if(price >= 20 && price <= 30){
         return 1;
      }

      else{
         return 0;
      }
   }

    /*
     * calculates basic pricing for total number of pizza
     * @return double
     */
   public static double calculateCost(int shape, int diameter, int length, int width, int numToppings, int typeDough){

      double a = area(shape, diameter, length, width);
      double v = volume(a, typeDough);
      double cost = a * (0.036  + numToppings * 0.025 ) + v * 0.019;
      return cost;
   }

    /*
     * calculates area of a pizza
     * @return double
     */
   public static double area(int shape, int diameter, int length, int width){

      double a = 0.0;

      if(shape == 1){ // round pizza
         a = Math.PI * (diameter / 2) * (diameter / 2);
      }

      else if(shape == 2){ // rectangular pizza
         a = length * width;
      }

      return a;
   }

    /*
     * calculates volume of a pizza
     * @return double
     */
   public static double volume(double a, int typeDough){

      double height = 0.0;

      switch(typeDough){

         case 1:
            height = 0.1;
            break;

         case 2:
            height = 0.25;
            break;

         case 3:
            height = 0.5;
            break;

         case 4:
            height = 0.9;
            break;
      }

      return a * height;
   }

    /*
     * calculates cost for cheesy crust
     * @return double
     */
   public static double calculateChessyCrustCost(int shape, int diameter, int length, int width){
      return size(shape, diameter, length, width) * 0.02;
   }



    /*
     * calculates size of pizza
     * @return double
     */
   public static double size(int shape, int diameter, int length, int width){
      if(shape == 1){ // round pizza
         return 2 * Math.PI * (diameter / 2);
      }

      else{ // rectangular pizza
         return 2 * (length + width);
      }
   }

   public static void main(String[] args) {

      int shape; // 1 => round, 2 => rectangular
      int length = 0;
      int width = 0;
      int diameter = 0;
      int numToppings = 0; // number of toppings
      int typeDough; // 1 => thin & crusty, 2 => hand tossed, 3 => pan, 4 => texas toast
      boolean cheesyCrust = false; // true => add, false => don't add
      int lengthCrust = 0;
      int numPizza; // number of pizzas ordered
      int orderType; // 1 => delivery, 2 => take-out

      Scanner scanner = new Scanner(System.in);

      System.out.println("This program helps you to order pizza based on your personal preferences.");
      System.out.println("It notes your choices and calculates total cost for you, including tax and even delivery fee if applicable.");
      System.out.println("Please fill out information for the following:");
      System.out.println();
      System.out.println("Pizza style:");
      System.out.println("1. Round Pizza");
      System.out.println("2. Rectangular Pizza");




      shape = scanner.nextInt();

      if(shape == 1){
         System.out.println("Diameter:(inches)");
         diameter = scanner.nextInt();
      }

      else if(shape == 2){
         System.out.println("Length:(inches)");
         length = scanner.nextInt();
         System.out.println("Width:(inches)");
         width = scanner.nextInt();}
      else {throw new IllegalArgumentException("Enter enter 1 fo Round pizza or 2 for rectangualar pizza"); 
      }

      System.out.println("Number of toppings:");
      numToppings = scanner.nextInt();

      System.out.println("Type of dough:");
      System.out.println("1. Thin & Crusty");
      System.out.println("2. Classic Hand Tossed");
      System.out.println("3. Pan");
      System.out.println("4. Texas Toast");
      typeDough = scanner.nextInt();

      if(typeDough != 1){
         System.out.println("Do you want to add cheest crust?[true/false]");
         cheesyCrust = scanner.nextBoolean();


      }

      System.out.println("How many pizzas do you want to order?");
      numPizza = scanner.nextInt();
      System.out.println("Select your receival method:");
      System.out.println("1. Delivery");
      System.out.println("2. Take away");
      orderType = scanner.nextInt();

      double a = area(shape, diameter, length, width);
      double v = volume(a, typeDough);
      double baseCost = calculateCost(shape, diameter, length, width, numToppings, typeDough);
      double crustCost = 0.0;

      if(cheesyCrust == true){
         crustCost = calculateChessyCrustCost(shape, diameter, lengthCrust, width);
      }

      double costOne = calculatePriceWithTax(baseCost + crustCost);
      double deliveryCharge = 0.0;

      if(orderType == 1){ // delivery
         costOne += deliveryFee(costOne);
      }

      double totalCost = deliveryFee(costOne * numPizza) + (costOne * numPizza);

      System.out.print("Area: ");
      System.out.printf("%.2f", a);
      System.out.print(" (inches square)");
      System.out.println();
      System.out.print("Volume:");
      System.out.printf("%.2f", v);
      System.out.print(" (cubic inches)"); 
      System.out.println(); 
      System.out.print("Base Cost: ");
      System.out.printf("%.2f", baseCost);
      System.out.print(" dollar"); 
      System.out.println();
      System.out.print("Cost for one pizza: ");
      System.out.printf("%.2f", costOne);
      System.out.print(" dollar"); 
      System.out.println();
      System.out.print("Your order has been processed. Total cost including taxes: ");
      System.out.printf("%.2f", totalCost );
      System.out.print(" dollars"); 
      System.out.println();
      System.out.println("Thank you for using our service.");
   }
}
圆形比萨饼的
:e=2π(d/2)(周长)
对于矩形比萨饼:e=2(L+w)(周长)
哪里
e=比萨饼皮的大小(英寸)
d=圆形比萨饼的直径(英寸)
L=矩形比萨饼的长度(英寸)
w=矩形比萨饼的宽度(英寸)
在这里输入代码
导入java.util.Scanner;
公共级比萨饼{
/*
*计算含税价格
*@return-double
*/
公共静态双重计算含税价格(双重价格){
退货价格*(1+0.07);
}
/*
*计算送货费(如有)
*@return-double
*/
公共静态双重交付费(双倍价格){
如果(价格<10){
返回3;
}
否则,如果(价格>=10&&price=20&&price四舍五入,2=>矩形
整数长度=0;
整数宽度=0;
内径=0;
int numToppings=0;//浇头数
int-typeruam;//1=>薄而硬,2=>手抛,3=>平底锅,4=>德克萨斯土司
布尔cheesecrust=false;//true=>添加,false=>不添加
int lengthCastle=0;
int numPizza;//订购的比萨饼数量
int orderType;//1=>delivery,2=>take out
扫描仪=新的扫描仪(System.in);
System.out.println(“此程序帮助您根据个人喜好订购比萨饼。”);
System.out.println(“它记录您的选择并为您计算总成本,包括税费,甚至包括送货费(如果适用)”;
System.out.println(“请填写以下信息:”);
System.out.println();
System.out.println(“比萨饼风格:”);
System.out.println(“1.圆形比萨饼”);
System.out.println(“2.矩形比萨饼”);
shape=scanner.nextInt();
如果(形状==1){
系统输出打印长度(“直径:(英寸)”;
直径=扫描器.nextInt();
}
else if(形状==2){
System.out.println(“长度:(英寸)”;
长度=scanner.nextInt();
系统输出打印长度(“宽度:(英寸)”;
宽度=scanner.nextInt();}
否则{抛出新的IllegalArgumentException(“输入1个圆形比萨饼或输入2个直肠比萨饼”);
}
System.out.println(“配料数量:”);
numToppings=scanner.nextInt();
System.out.println(“面团类型:”);
System.out.println(“1.薄而硬”);
System.out.println(“2.经典手抛”);
System.out.println(“3.Pan”);
System.out.println(“4.Texas Toast”);
typeruam=scanner.nextInt();
如果(类型面团!=1){
System.out.println(“是否要添加干酪皮?[true/false]”;
cheesecrust=scanner.nextBoolean();
}
System.out.println(“您要订购多少比萨饼?”);
numPizza=scanner.nextInt();
System.out.println(“选择您的接收方法:”);
系统输出打印(“1.交付”);
System.out.println(“2.外卖”);
orderType=scanner.nextInt();
双a=面积(形状、直径、长度、宽度);
double v=体积(a,面团);
双基成本=计算成本(形状、直径、长度、宽度、numToppings、类型);
双倍成本=0.0;
if(cheesecrust==true){
结壳成本=计算的结壳成本(形状、直径、长度、结壳宽度);
}
双成本一=计算含税价格(基本成本+成本);
双倍运费=0.0;
如果(orderType==1){//delivery
costOne+=送货费(costOne);
}
双倍总成本=送货费(costOne*numPizza)+(costOne*numPizza);
系统输出打印(“区域:”);
系统输出打印F(“%.2f”,a);
系统输出打印(“(平方英寸)”);
System.out.println();
系统输出打印(“卷:”);
系统输出打印F(“%.2f”,v);
系统输出打印(“立方英寸”);
System.out.println();
系统输出打印(“基本成本:”);
系统输出打印F(“%.2f”,基本成本);
系统输出打印(“美元”);
System.out.println();
系统输出打印(“一个比萨饼的成本:”;
System.out.printf(“%.2f”,costOne);
系统输出打印(“美元”);
System.out.println();
System.out.print(“您的订单已处理。包括税费在内的总成本:”);
系统输出打印F(“%.2f”,总成本);
系统输出打印(“美元”);
System.out.println();
System.out.println(“感谢您使用我们的服务”);
}
}

问题1在您的
区域
方法中:

a = Math.PI * (diameter / 2) * (diameter / 2);
在java中,
int/int=int
,因此必须将其转换为double

要解决此问题,请尝试以下操作:

a = Math.PI * (diameter / 2.0d) * (diameter / 2.0d);
同样的问题也存在于
size
方法中。它应该如下所示:

return 2 * Math.PI * (diameter / 2.0d);

a=Math.PI*(diameter/2)*(diameter/2);
您在这里执行整数除法。实际上,它给了我面积的正确计算!我的问题在于奶酪皮!如果您正在执行
a=Math.PI*(diameter/2)*(diameter/2);
diameter=1
(每个示例),它将给你0。因此,不确定它是否进行了正确的计算。我同意他们的看法。你的
size()
方法不可能返回正确的值,这是地壳成本计算的很大一部分。我只是解决了这个问题。仍然不起作用!我认为
2.0
就足够了。