2016-03-29 7 views
1

私は行列内の自由な位置を探すためのこの方法を持っています。これは私がmatrixを印刷したときの出力です。理由がないためにポイントxの値が変化します

1 0 0 0 0 0 0 
1 1 1 0 0 0 0 
1 1 1 0 0 0 0 
0 1 0 0 0 0 0 
0 0 0 0 0 0 0 
0 0 0 0 0 0 0 
0 0 0 0 0 0 0 

私は(0,0)currentPosを持っています。次の自由な位置は(0,1)でなければなりません。私がNextFreePos()と呼ぶと、それは変わるべきではないにしても、currentPos.xの値が変わるでしょう。

NEXT POS = (0;0) 
SAME X 
NEXT POS = (1;1) // I dont get why the x (first value) changed to 1 

public void nextFreePos(Point currentPos){ 
     // display the OLD POS (x,y) <-- x = row and y = column 
     System.out.println("OLD POS = ("+(currentPos.x)+";"+currentPos.y+")");  
     boolean stop = false; 
     // check if there is a free Pos in current row, starting from current y 
     for(int y = currentPos.y; y < 7; y++){ 
      // check if Position is free and no free position has been found yet 
      if(matrix[currentPos.x][y] == 0 && stop == false){ 
       stop = true; 
       System.out.println("SAME X"); 
       currentPos.y = y; 
      } 
     }   
     currentPos.x++;   
     for(int x = currentPos.x; x < 7; x++){ 
      for(int y = 0; y < 7; y++){ 
       // check if Position is free and no free position has been found yet 
       if(matrix[x][y] == 0 && stop == false){ 
        stop = true; 
        System.out.println("OTHER X"); 
        // set the position to x and y 
        currentPos.x = x; 
        currentPos.y = y; 
       }     
      } 
     } 
     // display the NEXT POS (x,y) 
     System.out.println("NEXT POS = ("+currentPos.x+";"+currentPos.y+")");  
    } 
+0

あなたは 'currentPos.x ++;でインクリメントします –

答えて

3
:私はこの方法を実行したときに私が得る出力され、私は2番目のforループに真を取得する場合、私は唯一の xを変更するが、私はここでループ のための第1の内側 stop = trueを作るので、これは常に、 falseを返します。

強調表示されているコードを参照してください。

NEXT POS = (0;0) 
SAME X 
NEXT POS = (1;1) // I dont get why the x (first value) changed to 1 

public void nextFreePos(Point currentPos){ 
     // display the OLD POS (x,y) <-- x = row and y = column 
     System.out.println("OLD POS = ("+(currentPos.x)+";"+currentPos.y+")");  
     boolean stop = false; 
     // check if there is a free Pos in current row, starting from current y 
     for(int y = currentPos.y; y < 7; y++){ 
      // check if Position is free and no free position has been found yet 
      if(matrix[currentPos.x][y] == 0 && stop == false){ 
       stop = true; 
       System.out.println("SAME X"); 
       currentPos.y = y; 
      } 
     }   
 currentPos.x++; 
 for(int x = currentPos.x; x < 7; x++){ 
      for(int y = 0; y < 7; y++){ 
       // check if Position is free and no free position has been found yet 
       if(matrix[x][y] == 0 && stop == false){ 
        stop = true; 
        System.out.println("OTHER X"); 
        // set the position to x and y 
        currentPos.x = x; 
        currentPos.y = y; 
       }     
      } 
     } 
     // display the NEXT POS (x,y) 
     System.out.println("NEXT POS = ("+currentPos.x+";"+currentPos.y+")");  
    } 
+0

これは愚かでした...私はstop == falseのときにそれを行うべきです。 – user2173361

2

あなたは++、ループの最初の後にあなたがcurrentPos.xを呼び出す;,(0,1)で、開放位置と設定し、停止=真を見つけます。 xをインクリメントする。 stopはすでにfalseであるため、次のループは決して何もしません。

関連する問題