0

why the code is showing error in initialisation of j?

public static void main (String args[]) { int td[][]= new int [10][10]; int i,j, k=0; for (i = 0; i<10;i++) for (j=0; j<10; j++) {td[i][j]=k; k++; } if (i== j) { for(i=0; i<10; i++) {for(j=0; j<10; j++) {System.out.print(td[i][j] + " "); }System.out.println( ); } } else if (i != j); System.out.println(); }

21st Aug 2016, 3:02 PM
Anshul Rawal
Anshul Rawal - avatar
4 odpowiedzi
+ 1
Because i and j and are local variables, which means they don't get assigned default values. You have to initialize them to something or in your for loops put int i = 0/ int j = 0.
21st Aug 2016, 3:42 PM
James
James - avatar
+ 1
int i=0,j=0, k=0;
23rd Aug 2016, 8:43 PM
kelvin kosgei
kelvin kosgei - avatar
0
yeah i was just about to say that.
21st Aug 2016, 3:45 PM
Aquarius
Aquarius - avatar
0
main method should be in class too public class Test{ public static void main (String args[]) { int td[][]= new int [10][10]; int i=0,j=0, k=0; for (i = 0; i<10;i++) for (j=0; j<10; j++) {td[i][j]=k; k++; } if (i== j) { for(i=0; i<10; i++) {for(j=0; j<10; j++) {System.out.print(td[i][j] + " "); }System.out.println( ); } } else if (i != j) System.out.println(); }}
21st Aug 2016, 4:04 PM
Tiger
Tiger - avatar