2017-11-12 5 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 

내 코드 : 나는 "bubbleI는 거품 배열의 인덱스 난에서 거품하자"라고 생각

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

아니라, Bubble bubbleI = bubbles[i]; 될한다 실제로 아무것도하지 않는 임무보다.

이 문 경우에 truefalse 비교하는 것도 이례적인 - foo == true 정확히 foo과 동일하고, 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();    
      } 
     }   
    } 
} 

또는 .. 말 그대로 의사 코드를 번역하거나, 대신에 관용구 코드를 쓰려고하는 것이 확실하다면 ..

관련 문제