Rispondi a: esercizi array

Blog Forum Java esercizi array Rispondi a: esercizi array

#5682
Luigi
Ospite
Up
0
Down
::

Salve, professor Pagano volevo sapere cosa c’è di sbagliato nel codice e perché mi esce il seguente risultato.
grazie per la disponibilità, cordiali saluti:
1 2 3 4 5 6 7 8 9 10
0 0 0 0 0 0 0 0 0 11
0 0 0 0 0 0 0 0 0 12
0 0 0 0 0 0 0 0 0 13
0 0 0 0 0 0 0 0 0 14
0 0 0 0 0 0 0 0 0 15
0 0 0 0 0 0 0 0 0 16
0 0 0 0 0 0 0 0 0 17
97 98 0 0 0 0 0 0 0 18
100 99 26 25 24 23 22 21 20 19
codice

package ese;

public class e22 {

public static void main(String[] args) {
int n = 10;
System.out.println(“Spiral pattern is: \n”);
printSpiralpattern(n);

}

private static void printSpiralpattern(int n) {
int row = 0, col = 0;
int b = n -1;
int s = n -1;
int f = 1;
char m = ‘r’;
int [][] a = new int [n][n];
for(int i = 1; i < n*n+1; i++)
{
a[row][col] = i;
switch(m) {
case ‘r’:
col += 1;
break;
case ‘l’:
col -= 1;
break;
case ‘u’:
row -= 1;
break;
case ‘d’:
row += 1;
break;
}
if (i == b) {
b = b + s;
if (f != 2) {
f = 2;
}
else {
f = 1;
s = 1;
}
switch (m)
{
case ‘r’:
m = ‘d’;
break;
case ‘d’:
m = ‘l’;
break;
case ‘l’:
m = ‘u’;
break;
case ‘u’:
m = ‘r’;
break;

}

}
}
for(row = 0; row < n; row++) {
for(col = 0; col < n; col++) {
int o = a[row][col];
if(o < 100)
System.out.print(o + ” “);
else
System.out.print(o + ” “);
}
System.out.println();
}

}

}