2014-01-08 2 views
0

중첩 루프를 만들어 특정 시간을 표시 할 수 있지만 한 가지 요소 만 배치하고 다른 요소는 배치하지 않는 것이 문제입니다. 어떻게 좌표를 설정합니까? 당신이 원 위치를 하드 코딩하고, 따라서 모든 원에 대해 동일합니다 아마 때문에중첩 루프 만들기

int rows, cols; 
rows = 7; 
cols = 6; 

for (int i=0; i <rows; i++); { 
    for (int j=0; j< cols; j++); 

} 

답변

4

?

canvas.drawCircle(80, 155, 40, white); 

위치를 계산하는 방법은 무엇입니까?

int x = i * radius + 10; // or whatever you wanna calculate here... 
int y = j * radius + 10; 
canvas.drawCircle(x, y, 40, white); 
+0

루프 내부 같은 즉시'에 의해 폐쇄를 시도하기 때문에 원의 위치를 ​​변경 doesen't; ': P – Muel

+0

나를 때려! 바깥 쪽 루프에도 세미콜론이옵니다. – Rafa

0

각 루프 사이클에서 원 좌표를 업데이트해야합니다! 지금 그들은 하드 코딩되어 있습니다. 이 도움이 될 수

int rows, cols; 
rows = 7; 
cols = 6; 
for (int i=0; i <rows; i++); { 
    for (int j=0; j< cols; j++) { 
    canvas.drawCircle(80 + (80*i), 155 + (155*j), 40, white); 
    } 
} 

난 그냥 오프셋으로 x와 y에 대해 동일한 값을 사용, 당신은 당신이 가고있는 모습을 찾기 위해 그들 (80155) 함께 놀러 할 수도있다.

0

귀하의 루프는 80과 155 에 그릴 말 또한이

int rows, cols; 
rows = 7; 
cols = 6; 
//initial x and y positions 
float initialX =80; 
float initialY =155; 
int radius = 40; 
for (int i=0; i <rows; i++); { 
    //you need to reset it for each circle 
    float drawX = initialX; 
    for (int j=0; j< cols; j++){ 
     canvas.drawCircle(initialX, initialY, radius, white); 
     //where 10 will be the little space you want between circles 
     drawX+=radius*2+10; 
    } 
    initialY+=radius*2+10; 
}