English 中文(简体)
无法对两条插图进行正确比较
原标题:Unable to compare two strings correctly

现在,我可以通过我可能有效的渠道,成功地使我无法在我的方案中增加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;}
    }
  }
}

我看不出为什么不平等的方法没有奏效,也没有把变化的奇数分配给最终价格......

// 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()
{
  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"};

当地是建筑商,与全球有效图谋没有任何联系。 查阅。

问题回答

The validTopics instance array in Pizza is never filled. Replace

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

in Pizza s constructor with:

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

此外,在你的法典中有一些奇怪的内容:

  • addTopping is fairly inefficient regarding the iteration over the toppings
  • addTopping uses a (IMO) magic number in the for loops => 3
  • you initialize the arrays in Pizza twice
  • the way you initialize and validate the different toppings is confusing

页: 1 你们将每项努力比较为无效。

您以这种方式宣布<代码>Pizza:

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

and you re trying to assign a value in the constructor this way

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

但是,建筑商的上述一切都造成了一种新的地方变量,在施工者被处决后将放弃这一变量。

The fix is to assign a value to the members. For instance, one possible proper solution would be the following:

  • Declare members like this: String[] validToppings;
  • And assign a value in the constructor like this: validToppings = {"mushroooms", ... };

从设计角度看(青年可能是一个开端人,但仍值得一提),你应当使用点数(如果能避免你对地体进行比较,打字,......)。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签