2017-11-12 3 views
0

私の仕事を提出する前に、以下に書いたコードが正しく翻訳されていることを確認したいだけです。この方法はうまくいきますが、何か間違ったことを書いたような気がします。この疑似コードを正しく変換しましたか?

擬似コード:

assign i the value 0 

WHILE i is less than the length of the array minus 1 

let bubbleI be the bubble at index i in the bubbles array 

assign j the value i + 1 

WHILE bubbleI is not popped AND j is less than the length of the bubbles 
array 

let bubbleJ be the bubble at index j in the bubbles array 
IF bubbleJ is not popped AND bubbleI is touching bubbleJ 
pop both bubbleI and bubbleJ 

END IF 

increment j by 1 

END WHILE 

increment i by 1 

END WHILE 

マイコード:

private void popAll() { 

    int i = 0; 

    while (i < bubbles.length - 1){ 

     bubbles[i] = bubbles[i]; 
     int j = i + 1; 

     while (bubbles[i].isPopped() == false && j < bubbles.length){ 

      bubbles[j] = bubbles[j]; 

      if (bubbles[j].isPopped() == false && bubbles[i].isTouching(bubbles[j]) == true){ 

       bubbles[i].pop(); 
       bubbles[j].pop();    
      } 
      j++;   
     }   
     i++; 
    } 
} 

答えて

2

私は "私は泡のアレイにbubbleIがインデックスでバブルなりましょう" は、むしろ、Bubble bubbleI = bubbles[i];になるべきだと思います実際に何もしないその割り当てよりも。これは、if文でtruefalseに比較するのも珍しい

- foo == truefooとまったく同じであり、foo == false!fooと全く同じです。

最後に、しばらくは初期設定でループし、増分がfor文がためですまさにしているので、私はこのような全体のこと書きたい:

private void popAll() { 
    for (int i = 0; i < bubbles.length - 1; i++) { 
     Bubble bubbleI = bubbles[i]; 

     for (int j = i + 1; !bubbleI.isPopped() && j < bubbles.length; j++) { 
      Bubble bubbleJ = bubbles[j]; 

      if (!bubbleJ.isPopped() && bubbleI.isTouching(bubbleJ)) { 
       bubbleI.pop(); 
       bubbleJ.pop();    
      } 
     }   
    } 
} 

それとも、あなたはしばらくループを維持することができますが...文字通り擬似コードを翻訳するか、代わりに慣用コードを書くことが期待されるのかは不明です。

関連する問題