My code takes 2 input numbers to create the rows and columns of a rectangle. In that rectangle, the user chooses 2 letters for the rectangle body but they have to alternate, like this: (without the dashes ofc)
-xxxxx
-r r r r r
-xxxxx
-r r r r r
Whenever I try to make them alternate, the rows usually end up like "xrxrx" or "xrrrr" instead of a single letter. I've tried adding them "filler.append(firstLetter + secLetter);" but it just results in numbers. This probably has a really easy solution but I just don't see it ha ha...(・_・;) Any hints would be greatly appreciated, thank you!
public static void recDraw ( int rows, int columns, char firstLetter, char secLetter){
StringBuilder filler = new StringBuilder();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
filler.append(firstLetter);
filler.append(secLetter);
}
filler.append('\n');
}
System.out.println(filler);
}
Inside inner loop (with counter j), before appending the letter to StringBuilder filler, check whether it is Fistletter line or SecondLetter line by using modulus operation on i value. 'i' value is used because it represents the line/row.
public static void recDraw ( int rows, int columns, char firstLetter, char secLetter){
StringBuilder filler = new StringBuilder();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
if(i%2 == 0){
filler.append(firstLetter);
} else{
filler.append(secLetter);
}
}
filler.append('\n');
}
System.out.println(filler);
}