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(); }
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.
+ 1
int i=0,j=0, k=0;
0
yeah i was just about to say that.
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();
}}