2012-04-23 42 views
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; 
    }; 

} 
+0

为什么不使用HashMap并使搜索和存储容易? – 2012-04-23 07:06:38

回答

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 []'。了解更多。

4

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

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

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

此外,还有在你的代码的一些奇怪的事情:

  • addTopping是关于迭代在相当低效浇头
  • addToppingfor中使用(IMO)幻数环路=> 3
  • 你在Pizza初始化阵列两次
  • 初始化的方式和验证不同的浇头是混乱
+0

这可能是票。感谢指针兄弟。让我试试这个,编译并发布结果! – user1251814 2012-04-23 06:44:43

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", ... };

从设计的角度看(你可能是一个初学者,但还是值得一提),你应该使用枚举(会救你比较字符串,使错别字,...)。