Reputation: 1
This code should draw table, but it doesn't. Why? The code compiles but it doesn't print enything. Here is code:
import java.util.Arrays;
public class Nizovi{
public static char table[][]= new char[10][10] ;
public static void drawTable(){
// this should draw table
int k=1;
while(k <= 30){
System.out.print("-");
}
System.out.println();
for(int i=0; i < table.length; i++){
for(int j=0; j < table[i].length; j++){
System.out.print("|"+ table[i][j] + "|");
}
System.out.println();
}
k=1;
while(k <= 30){
System.out.print("-");
}
}
public static void buildTable(){
// and this is supposed to fill it with *
for(char[] row: table){
Arrays.fill(row, '*');
}
}
public static void main (String[] args){
Nizovi.buildTable();
Nizovi.drawTable();
}
}
I can't see what i miss. What's wrong here?
Upvotes: 0
Views: 84
Reputation: 6523
Increment k
inside the while blocks:
while(k <= 30){
System.out.print("-");
k++; // add this to your loops
}
In your code, k
is not updated within the loops, it therefore remains 1
and stays always less-or-equal to 30
(k <= 30
always yields true
)
know as "an endless loop"
Output with incrementing the k
references within the while
-blocks:
------------------------------
|*||*||*||*||*||*||*||*||*||*|
|*||*||*||*||*||*||*||*||*||*|
|*||*||*||*||*||*||*||*||*||*|
|*||*||*||*||*||*||*||*||*||*|
|*||*||*||*||*||*||*||*||*||*|
|*||*||*||*||*||*||*||*||*||*|
|*||*||*||*||*||*||*||*||*||*|
|*||*||*||*||*||*||*||*||*||*|
|*||*||*||*||*||*||*||*||*||*|
|*||*||*||*||*||*||*||*||*||*|
------------------------------
(Make sure you update both while
-blocks (hence the plural))
Upvotes: 2
Reputation: 31841
increment k in while loop
while(k <= 30){
System.out.print("-");
k++;
}
Upvotes: 0
Reputation: 639
The while
loop is different from for
loop. for
assumes you are going to do something a certain amount of times and can therefore automatically increment the index by executing the i++
part. while only checks if the condition is fulfilled. Therefore you should take care of the state of the condition and increment the counter k
in the body of the while
loop by yourself.
Upvotes: 0
Reputation: 6395
Your loop says while(k <= 30)...
- how is k ever to reach 30? Nothing is changing it.
Upvotes: 4