Java dead code warning + no rectangle forming?
The first return
will terminate the method on the first iteration of the loop. Instead of all those returns, you should append to the string and only return at the end:
public static String rectangle(int row, int col, char thing) {
String result = "";
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
String character = "" + thing;
result += character;
}
result += "\n";
}
return result
}
Having said that, using a StringBuilder
would probably be more performant:
public static String rectangle(int row, int col, char thing) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
result.append(thing);
}
result.append("\n");
}
return result.toString();
}
Read more here: Source link