0

现在我可以成功地遍历可能的有效浇头循环,我无法在我的程序中添加 0.75 的浇头附加费。我希望你能告诉我为什么会这样。整个程序在底部。我关心的两个部分是

public class TestPizza
{
  public static void main(String args[])
  {
    int x;
    String top[] = {"Mushrooms", "Onions", ""};
    Pizza one = new Pizza();
    Pizza two = new Pizza();
    Pizza three = new Pizza();

one.setSize(12);
one.addTopping(top);
one.showValues();

  }
}

// setPrice() assigns a price to the pie
public void addTopping(String programToppings[])
{
  for(int x = 0; x < 3; x++)
  {
    toppings[x] = programToppings[x];
  }
  for(int x = 0; x < 3; x++)
  {
    toppings[x] = toppings[x].toLowerCase();
  }
  for(int x = 0; x < 3; x++)
  {
    for(int xx = 0; xx < 6; xx++)
    {
      if(toppings[x].equals(validToppings[xx]))
      {price += 0.75;}
    }
  }
}

我不明白为什么 .equals 方法不起作用并将奇数的变化总和分配给最终价格......

// This custom class is used to create Pie objects
// It stores the data about the Pie in four variables:
// size, price, type and baked
// It lets the program that creates the Pie object set these values using four methods:
// setSize, setPrice, setType and bake

public class Pizza
{

  // Declare four variables that can store the values for each pie
  // Each Pie object will have their own, separate copy of these variables 12.
    private int size;
    private double price;
    private boolean baked;
    private int x;
    private int xx;
    private String validToppings[] = new String[6];
    private String toppings[] = new String[3];


    // The "constructor" method is called when a new pie
    // object is first created. We use it to set "default" values.
    // Our typical pie is 10 inches, costs $8 and is not baked yet
    // We don't yet know what the pie filling will be
    Pizza()
    {
      size = 8;
      price = 10.0;
      baked = false;
      String validToppings[] = {"mushrooms", "pepperonis", "onions", "mushroom", "pepperoni", "onion"};
      String toppings[] = new String[3];
    }

    // showValues() is a void method that displays the values of the
    // current Pie
    public void showValues()
    {
      System.out.println("Pie Size: " + size);
      for(int x = 0; x < 3; x++) {System.out.println("With " + toppings[x]);};
      System.out.println("Price of Pie: $" + price);
    }

    // getSize() returns the size of the pie
    public int getSize()
    {
      return size;
    }
    // getPrice() returns the price of the pie
    public double getPrice()
    {
      return price;
    }
    // baked() returns whether or not the pie is baked
    public boolean getBaked()
    {
      return baked;
    }
    // setSize() assigns a size to the pie
    public void setSize(int thisSize)
    {
      size = thisSize;
      switch(size)
      {
        case 8: price = 10.00; break;
        case 12: price = 14.00; break;
        case 16: price = 18.00; break;
        default: System.out.println("Error in Pizza class: Attempt to set invalid Pizza size."); break;
      }
    }

    // setPrice() assigns a price to the pie
    public void addTopping(String programToppings[])
    {
      for(int x = 0; x < 3; x++)
      {
        toppings[x] = programToppings[x];
      }
      for(int x = 0; x < 3; x++)
      {
        toppings[x] = toppings[x].toLowerCase();
      }
      for(int x = 0; x < 3; x++)
      {
        for(int xx = 0; xx < 6; xx++)
        {
          if(toppings[x].equals(validToppings[xx]))
          {price += 0.75;}
        }
      }
    }

  public void bake()
  {
    baked = true;
  };

}
4

3 回答 3

4

Pizza 中的validTopics实例数组永远不会被填充。代替

String validToppings[] = {"mushrooms", "pepperonis", "onions", "mushroom", "pepperoni", "onion"};

inPizza的构造函数:

this.validToppings[] = {"mushrooms", "pepperonis", "onions", "mushroom", "pepperoni", "onion"};

此外,您的代码中有许多奇怪的东西:

  • addTopping关于浇头的迭代效率相当低
  • addToppingfor在循环中使用 (IMO) 幻数=> 3
  • 你初始化数组Pizza两次
  • 您初始化和验证不同浇头的方式令人困惑
于 2012-04-23T06:42:21.037 回答
0
 Pizza()
{
  size = 8;
  price = 10.0;
  baked = false;
  String validToppings[] = {"mushrooms", "pepperonis", "onions", "mushroom", "pepperoni", "onion"};
  String toppings[] = new String[3];
}

这里,

String validToppings[] = {"mushrooms", "pepperonis", "onions", "mushroom", "pepperoni", "onion"};

对于构造函数来说是本地的,并且无法连接到全局“validToppings[]”。了解更多。

于 2012-04-23T07:09:15.533 回答
0

你的validToppings数组是空的。您正在将每个字符串与 null 进行比较。

Pizza您正在以这种方式声明成员:;

private String validToppings[] = new String[6];
private String toppings[] = new String[3];

并且您正在尝试以这种方式在构造函数中分配一个值

String validToppings[] = {"mushrooms", "pepperonis", "onions", "mushroom", "pepperoni", "onion"};
String toppings[] = new String[3];

但是上面的代码从构造函数所做的一切都是创建一个新的局部变量,在构造函数执行完成后将被遗忘。

解决方法是为成员分配一个值。例如,一种可能的适当解决方案如下:

  • 像这样声明成员: String[] validToppings;
  • 并在构造函数中分配一个值,如下所示: validToppings = {"mushroooms", ... };

从设计的角度来看(您可能是初学者,但仍然值得一提)您应该使用枚举(可以避免比较字符串、打错字……)。

于 2012-04-23T06:47:32.037 回答