I have the exercise in which I am asked to draw a cross depending on the number of rows and columns that are requested by the console
I did the general part, that is, the simple part, but I am missing the main part, the one that draws the cross
I have tried several things, but I get strange things when printing
Someone could help me ?
code:
public class CruzArray {
public static void main(String[] args) {
Scanner teclado = new Scanner(System.in);
int f,c;
System.out.print("Ingrese el número de filas: ");
f = teclado.nextInt();
System.out.print("Ingrese el número de columnas: ");
c = teclado.nextInt();
System.out.println();
char[][] matriz = new char [f][c];
int mitadFilas = matriz.length / 2;
int mitadColumnas = matriz[0].length / 2;
for (int i = 0; i < matriz.length; i++) {
for (int j = 0; j < matriz[0].length; j++) {
matriz [i][j] = '*';
matriz [i][j] = ' ';
System.out.print(matriz[i][j] + " ");
}
}
}
}
The way I found to do it is by adding 1 column or row to what the user indicates, because to make a cross it must have the same length starting from the center and in case the number is even there will be an error in how the cross is assembled, to that's a ternary operator to simplify the code, the rest is just filling the array depending on whether it matches that it's half.
You do not specify anything in your question, in the way that the problem has been presented to you, and that is, that if the user indicates an even number as the number of rows or columns, the cross would not be regular, since it would seem displaced towards the right.
I propose a solution, which would add an extra row or column, if the user indicates that even number in them, and we indicate it by means of a text message.
Besides, I complete the code that would go inside the loops.
In my case I limit myself to validating that when the double loop finds half of the rows or columns, it writes the asterisk.
If not half is found in each iteration, type a blank.
I hope it helps you