2012-04-23 6 views
0

を比較することができません。私はそれがなぜあなたに教えてくれることを望んでいました。プログラム全体が一番下にあります。私の懸念は2つの部分が今、私は成功し、私は私のプログラムでトッピング用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を使用して検索と保存を簡単にするのはなぜでしょうか? –

答えて

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

validTopicsインスタンスの配列が満たされることはありません。 Pizzaのコンストラクタで

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

を交換してください:

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

さらに、あなたのコード内で奇妙なことがいくつかあります:

  • addToppingはイテレーションに関するかなり非効率的ですトッピング
  • addToppingforに(IMO)マジックナンバーを使用していますループ=> 3
  • あなたが初期化Pizza二回
  • 方法で配列を初期化し、さまざまなトッピングを検証するには、あなたのvalidToppings配列が空である
+0

これはチケットかもしれません。ポインタの弟に感謝します。私はこれを試してコンパイルし、結果を投稿しましょう! – user1251814

0

を混乱さ。各Stringを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]; 

しかし、コンストラクタから、上記のコードが実行後に忘れてしまったことになる新しいローカル変数を作成しているんすべてを値を代入しようとしていますのコンストラクタが完了します。

修正点は、メンバーに値を割り当てることです。例えば、一つの可能​​な適切な解決策は、以下のようになります。デザインの観点から validToppings = {"mushroooms", ... };

String[] validToppings;

  • そして、このようなコンストラクタで値を割り当てます。このような

    • 宣言メンバー(あなたは初心者かもしれませんが、言及するにはまだ価値があります)列挙型を使用する必要があります。ここ

  • 関連する問題