2012-04-23 33 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", ... };

從設計的角度看(你可能是一個初學者,但還是值得一提),你應該使用枚舉(會救你比較字符串,使錯別字,...)。