java - 无法正确比较两个字符串

标签 java arrays list string-comparison

现在我可以成功地迭代可能的有效配料循环,我无法在程序中添加 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;
  };

}

最佳答案

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

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

Pizza 的构造函数中:

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

此外,您的代码中还有许多奇怪的事情:

  • addTopping 对于配料的迭代效率相当低
  • addToppingfor 循环中使用 (IMO) 魔数(Magic Number) => 3
  • 您初始化了 Pizza 中的数组两次
  • 初始化和验证不同配料的方式令人困惑

关于java - 无法正确比较两个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10275870/

相关文章:

java - 重用具有最终实例字段的构造函数

java - 为什么 UIComponent 中的 encodeXxx 方法接受 FacesContext 参数?

arrays - Ruby 中 Hash 和 Array 哪个效率更高

angularjs - 使用 LoDash 合并包含相同键/值的对象

c# - ToArray() 方法将所有实例克隆为最后一个

python - 将列表项转换为定义的数据类型

java - 设置与方法参数同名的实例变量?

java - 如何检查 3D 对象是否与 OpenGL ES 2 中的射线相交 - Android

javascript - 从数组jquery中的子字符串中删除项目

Python变量列表人口