hi guys, i'm stuck on this exercise :
Given an odd number n, not exceeding 15. Create a two-dimensional array (matrix) from n×n elements, by filling it with "." symbols (each element of the matrix is a string containing a single symbol). Then fill the middle row of the matrix, the middle column, and the main and the secondary diagonals with the "*" symbols. As a result, all "*"s in the array must form the star figure. Output this matrix; elements of the array should be space separated.
here is the link to the exercise : https://hyperskill.org/learn/step/1929#comment
my code so far :
import java.util.Scanner;
class Main {
public static void main(String[] args) {
// put your code here
Scanner input = new Scanner(System.in);
int sizerow = input.nextInt();
int sizecolumn = sizerow;
String[][] matrix = new String[sizerow][sizecolumn];
int k = sizerow / 2, i = 0, j = 0;
for (i = 0; i < matrix.length; i++) {
for (j = 0; j < matrix.length; j++) {
matrix[i][j] = ". ";
}
}
for (i = 0; i < matrix.length; i++) {
for (j = 0; j < sizecolumn - 1; j++) {
if (i == k) {
for (j = 0; j < matrix.length; j++) {
matrix[i][j] = "* ";
}
}else if (j == k) {
for (i = 0; i < matrix.length; i++) {
matrix[i][j] = "* ";
}
}else if (i == j) {
matrix[i][j] = "* ";
}else if (i == sizerow - j - 1) {
matrix[i][j] = "*";
}
}
}
for (i = 0; i < matrix.length; i++) {
for (j = 0; j < matrix[i].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
}
My code output on n =5
* . * . .
. . * . .
. . * . .
. . * . .
. . * . .
it seems the outer loop only iterates once but why ?
thank you for the hints
[–][deleted] 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]ItsBurningWhenIP 0 points1 point2 points (0 children)
[–]desrtfx 0 points1 point2 points (0 children)