【问题标题】:Pizza Ordering Program比萨订购计划
【发布时间】:2016-03-16 19:56:39
【问题描述】:
public class PizzaEx {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
   char letter;
   String input;
   int sizeD;
   int pizzaCount=1;
   Pizza pieOne;

   do{
       sizeD = getValidSize();
       input = JOptionPane.showInputDialog(null, "What type of topping do you wish to order? " +
                                                 "\n Green Pepper" +
                                                 "\n Mushroom"+
                                                 "\n Sausage"+
                                                 "\n Pepperoni"+
                                                 "\n Plain");

       pieOne = new Pizza(sizeD, input);
       System.out.println(pieOne);
       System.out.println("The Number of pizzas made are " + pieOne.getPizzaCount() +"."+"\n");
       input = JOptionPane.showInputDialog(null, "Do you wish to continue?\n "+
                                                 "'y' or 'Y' for YES\n"+
                                                 "'n' or 'N' for NO\n");
       letter = input.charAt(0);

           pizzaCount = pizzaCount +1;
   }
   while (letter == 'Y'|| letter == 'y');

   System.exit(0);
}

private static int getValidSize()
{
    int d;
    String input;
    do{
        input = JOptionPane.showInputDialog(null, "What size of pizza do you wish to order? "+
                                                  "\n 9 inch"+
                                                  "\n 12 inch"+
                                                  "\n 16 inch");
        d = Integer.parseInt(input);
    } while (!(d==9 || d==12 || d==16));
    return d;
}

以上是我的主要课程

public class Pizza {



    private int diameter;
    private int numOfPizza;
    private double price;
    private String tops;

Pizza(int sizeD, String input) {
    diameter = sizeD;
    tops = input;

}

public int getDiameter(){
    return diameter;
}

/**
 *
 * @param pizzaCount
 * @return
 */
public int getPizzaCount(){
    return numOfPizza;
}
public double getPrice(){
    return price;
}
public String getToppings(){
    return tops;
}
public void setDiameter(int sizeD){
    if (sizeD == 9)
        diameter = 9;
    else if ( sizeD == 12)
        diameter = 12;
    else if (sizeD == 15)
        diameter = 15;
    else
        diameter = 0;
    }    
public void setPizzaCount(int pizzaCount){
    numOfPizza = pizzaCount;
}
public void setPrice(double total){
    price = total;
}
public void setToppings(String input){
    if ("green pepper".equalsIgnoreCase(input))
        tops = "Green Pepper";
    else if ("mushroom".equalsIgnoreCase(input))
        tops = "Mushroom";
    else if ("sausage".equalsIgnoreCase(input))
        tops = "Sausage";
    else if ("pepperoni".equalsIgnoreCase(input))
        tops = "Pepperoni";
    else 
        tops = "Plain";
}
private double calculatePrice(int sizeD, String input){
    double total;
    if (sizeD == 9 && (tops).equalsIgnoreCase("plain")) 
        total = 5.95;
    else if (sizeD == 9)
        total = 6.95;
    else if (sizeD == 12  && (tops).equalsIgnoreCase("plain") )
        total = 7.95;
    else if (sizeD == 12)
        total = 8.95;
    else if (sizeD == 16 && (tops).equalsIgnoreCase("plain"))
        total = 9.95;
    else if (sizeD == 16)
        total = 10.95;
    else 
        total = 0.0;

    return total;
}
public String toString(){
    String pizzaString ="You have ordered a "+diameter + " inch pizza with "+tops +" toppings and a price of $"+ calculatePrice(diameter, tops);
    return pizzaString;
}

当我打印出来时,即使我设置了pizzaCount = 1,它仍然显示制作的比萨饼数量 = 0。此外,当它要求浇头时,如果我键入除了有效浇头选项{"green peppers", "mushroom", "sausage", "pepperoni", "plain"} 之外的任何字符串,它将将该字符串计为浇头,并且当它应该是任何不应该是{"green peppers", "mushroom", "sausage", "pepperoni"} 的东西时将被收取浇头的费用@ 应该考虑@ 987654326@

这不是家庭作业或测试问题。这是我的教授分发的一些额外练习,不是为了成绩。我只是想要一些帮助来澄清为什么没有为 String tops 分配 setToppings() 方法调用的值。

【问题讨论】:

  • 你在 PizzaEx 主方法中设置了 PizzaCount 等于 1。但是你打印出 pieOne.getPizzaCount()。这从 Pizza 类中的 numOfPizza 中提取,该类永远不会设置。
  • 所以如果我选择一个带有无效配料的 9 英寸披萨,如“橄榄”//打印输出将是你订购了一个 9 英寸的带橄榄配料的披萨,价格为 6.95 美元进行编辑以将“橄榄”更改为“普通”,因为橄榄不是有效选项。

标签: java methods constructor


【解决方案1】:

你总是得到0getNumOfPizza()的原因是因为你从不增加int numOfPizza,你只在main增加pizzaCount

至于topping,即使输入无效的字符串也要收取浇头的费用,是因为您在calculatePrice 中的逻辑,如果!equalsIgnoreCase("plain") 则收取浇头的费用。换句话说,除了"plain" 之外的任何东西都将被视为浇头。其实这个方法中的逻辑是不必要的复杂,我建议你简化一些if语句:

private double calculatePrice(int sizeD, String input){
        if(!(tops).equalsIgnoreCase("plain")) {
            total = 1;
        } else {
            total = 0;
        }

        if(sizeD == 9) {
            total += 5.95;
        }

        else if(sizeD == 12) {
            total += 7.95;
        }

        else if(sizeD == 16) {
            total += 9.95;
        }

        return total;
    }

【讨论】:

  • 感谢您的逻辑!甚至没有看到它,因为这是最好的方法,因为所有的浇头都值 1 美元额外
【解决方案2】:

您的班级Pizza 有一个字段private int numOfPizza;,您正在使用pieOne.getPizzaCount() 访问该字段。因为该字段尚未初始化(它是一个原始int),它的默认值为0。一种可能的解决方法,

private int numOfPizza = 1;

确保您正在考虑您感兴趣的计数;本地 countPizza 计数。一旦解决了,你也应该改变

pizzaCount = pizzaCount +1;

类似

pizzaCount += pieOne.getPizzaCount();

【讨论】:

    【解决方案3】:

    你有以下构造函数:

    Pizza(int sizeD, String input) {
        diameter = sizeD;
        tops = input;
    
    }
    

    如您所见,它不会从 setToppings 运行您的逻辑。

    它也没有设置numOfPizza,你也没有调用你的setPizzaCount()方法。所以这个类变量保持为0

    【讨论】:

      【解决方案4】:

      您似乎有两个不同的字段来保存比萨饼的数量。比萨类应该只保存有关单个比萨的数据,而另一个类应保存有关比萨数量的数据。在您的主类中,您将 PizzaCount 初始化为 1,然后尝试从 Pizza numOfPizza 字段中获取数字。

      编辑另外,在单独的说明中,您的主类有太多的内容。你应该把一些东西抽象出来放在方法中。

      【讨论】:

      • 我会更改主类,但这就是主类的编写方式,并作为示例的一部分提供给我,因此尽量保持原样
      【解决方案5】:

      打印时:

      System.out.println("The Number of pizzas made are " + pieOne.getPizzaCount() +"."+"\n");
      

      您正在访问 pieOne。让我们检查初始化:

      Pizza pieOne = new Pizza(sizeD, input);
      

      sizeD 存储披萨大小(如 9),input 存储字符串。

      函数pieOne.getPizzaCount()检查pieOne内部并返回numOfPizza

      public int getPizzaCount(){
          return numOfPizza;
      }
      

      但由于您从未将该值实际存储在对象中,因此它将返回零!

      您可以在打印任何内容之前致电setPizzaCount()

      希望对你有帮助。

      【讨论】:

        【解决方案6】:

        无论用户每次键入什么,用于浇头的字符串都会返回的原因是因为 setToppings() 方法已声明但从未调用过。您需要在“tops”变量上调用它才能工作。实际上,程序完全跳过了该方法。

        这是您可以在程序中调用该方法的一种方式:

        public String toString()
        {
            //method call
            setToppings(tops);
            String pizzaString = "You have ordered a " + diameter + " inch pizza with " + tops + " toppings and a price of $" + calculatePrice(diameter, tops);
            return pizzaString;
        }
        

        }

        您的比萨饼数量问题与此相同。从未调用过 setPizzaCount() 方法,因此从未设置过 numOfPizza。由于 int 变量在未实例化时会自动默认为 0,因此每次都会得到 0 值。此外,当您将比萨饼的数量加一时,您可能需要考虑使用增量 (++) 运算符。它的存在是为了帮助您更轻松地编写代码并让其他人在递增数字时更容易理解。

        这是一个例子:

        do
            {
                sizeD = getValidSize();
                input = JOptionPane.showInputDialog(null, "What type of topping do you wish to order? " +
                        "\n Green Pepper" +
                        "\n Mushroom" +
                        "\n Sausage" +
                        "\n Pepperoni" +
                        "\n Plain");
                pieOne = new Pizza(sizeD, input);
                //method call
                pieOne.setPizzaCount(pizzaCount);
                System.out.println(pieOne);
                System.out.println("The Number of pizzas made are " + pieOne.getPizzaCount() + "." + "\n");
                input = JOptionPane.showInputDialog(null, "Do you wish to continue?\n " +
                        "'y' or 'Y' for YES\n" +
                        "'n' or 'N' for NO\n");
                letter = input.charAt(0);
                pizzaCount++;
            }
        

        【讨论】:

        • 谢谢你的例子。我的工作方式不同,但我更喜欢这种方式
        • 没问题,乐于助人。如果它对您有益,我们将不胜感激。
        猜你喜欢
        • 1970-01-01
        • 2012-09-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-22
        • 1970-01-01
        • 2015-06-16
        • 1970-01-01
        相关资源
        最近更新 更多