Saturday, 15 May 2010

java - Cannot insert an element in the concrete position and an infinite loop -



java - Cannot insert an element in the concrete position and an infinite loop -

i'm trying include char within bidimensional array, problem is, when write position element, code seems ignore , position 0, 0 default. here's code of method:

public boolean placepiece(int row, int col, char piece) { setpos(x, y); setpiece(piece); if (piece != 'k' && piece != 'c') homecoming false; else { board[x][y] = piece; homecoming true; } }

code of method setpos()

private void setpos(int x, int y) { this.x = x; this.y = y; if (x >= size && y >= size) { system.out.println("out of range."); } }

apart unusual thing that's happening, i'm getting infinite loop when executing method. method, counts 'k' in selected row , prints result user.

this.line = line; this.number = number; int counter = 0; if (line == 'r' || line == 'r') (int j = 0; j < size; j++) { while (board[number][j] == 'k') { counter += 1; system.out.println("you've found " + counter + " keys!"); } if (board[number][j] != 'k') { system.out.println("try again, haven't found key yet."); } }

don't know how rid of while , introduce construction instead.

hope can help me, greetings , in advance!

replace while if

for (int j = 0; j < size; j++) { if( board[number][j] == 'k') { ++counter; } } system.out.println("you've found " + counter + " keys!");

java arrays infinite-loop

No comments:

Post a Comment