英文:
Removing blank line from end of result in java
问题
这是我的结果显示:
1.*********
2.*********
3.*********
4.*********
5.*********
6.*********
我需要移除第7行的空行
这是我的代码:
public static String getRectangle(int maxRows, int maxCols, char symbol) {
new UnsupportedOperationException("您必须实现此方法。");
if (maxRows < 1 || maxCols < 1) {
return null;
} else {
String result = "";
for (int i = 0; i < maxRows; i++) {
for (int j = 0; j < maxCols; j++) {
result = result + symbol;
}
result = result + "\n";
}
return result;
}
}
英文:
This is what my result looks like:
1.*********
2.*********
3.*********
4.*********
5.*********
6.*********
7.
I need to remove the blank line 7
Here is my code:
public static String getRectangle(int maxRows, int maxCols, char symbol) {
new UnsupportedOperationException("You must implement this method.");
if (maxRows<1 || maxCols<1) {
return null;
} else {
String result = "";
for (int i = 0; i < maxRows; i++) {
for (int j = 0; j < maxCols; j++) {
result = result + symbol;
}
result = result + "\n";
}
return result;
}
}
答案1
得分: 3
在这里添加一个条件:
if (i != maxRows - 1) {
result = result + "\n";
}
虽然我建议使用 StringBuilder 而不是字符串来存储结果。
英文:
Add a condition here:
if (i != maxRows - 1) {
result = result + "\n";
}
Although i would suggest using a StringBuilder instead of a string to store the result.
答案2
得分: 1
你不需要任何额外的条件。只需将return result
替换为return result.trim()
。
英文:
You don't need any extra conditions. Just replace return result
with return result.trim()
答案3
得分: 1
自 Java 11 起,您可以使用 String::repeat
方法来创建具有指定行数和列数的矩形:
public static String getRectangle(int maxRows, int maxCols, char symbol) {
if (maxRows < 1 || maxCols < 1) {
return null;
} else {
String row = String.valueOf(symbol).repeat(maxCols); // 创建一行
String columns = (row + "\n").repeat(maxRows - 1); // 创建 n-1 列
return columns + row; // 追加最后一列
}
}
如果您使用的是 较低版本的 Java,可以使用 StringBuilder
。这个类的优点是它们可以相互配合使用:
public static String getRectangle(int maxRows, int maxCols, char symbol) {
if (maxRows < 1 || maxCols < 1) {
return null;
} else {
StringBuilder row = new StringBuilder();
StringBuilder column = new StringBuilder();
for (int i = 0; i < maxCols; i++) { // 行
row.append(symbol);
}
for (int i = 0; i < maxRows - 1; i++) { // n-1 列
column.append(row).append("\n");
}
return column.append(row).toString(); // 追加最后一行(不带换行符)
}
}
英文:
Since the Java version wasn't specified, as of Java 11, you can use String::repeat
with amended number of rows and columns:
public static String getRectangle(int maxRows, int maxCols, char symbol) {
if (maxRows<1 || maxCols<1) {
return null;
} else {
String row = String.valueOf(symbol).repeat(maxCols); // create a row
String columns = (row + "\n").repeat(maxRows - 1); // create n-1 columns
return columns + row; // append the last column
// .. without the new line
}
}
If you are stick with a lower version of Java, use StringBuilder
. The advantage of this class is they can be used with each other:
public static String getRectangle(int maxRows, int maxCols, char symbol) {
if (maxRows<1 || maxCols<1) {
return null;
} else {
StringBuilder row = new StringBuilder();
StringBuilder column = new StringBuilder();
for (int i = 0; i < maxCols; i++) { // rows
row.append(symbol);
}
for (int i = 0; i < maxRows - 1; i++) { // n-1 columns
column.append(row).append("\n");
}
return column.append(row).toString(); // append the last one w/o a new line
}
}
答案4
得分: 0
只需添加一个简单的条件即可
public static String getRectangle(int maxRows, int maxCols, char symbol) {
// new UnsupportedOperationException("You must implement this method.");
if (maxRows < 1 || maxCols < 1) {
return null;
} else {
String result = "";
for (int i = 0; i < maxRows; i++) {
for (int j = 0; j < maxCols; j++) {
result = result + symbol;
}
if (i != maxRows - 1) result = result + "\n";
}
return result;
}
}
英文:
Just adding a simple condition will help
public static String getRectangle(int maxRows, int maxCols, char symbol) {
// new UnsupportedOperationException("You must implement this method.");
if (maxRows < 1 || maxCols < 1) {
return null;
} else {
String result = "";
for (int i = 0; i < maxRows; i++) {
for (int j = 0; j < maxCols; j++) {
result = result + symbol;
}
if (i != maxRows - 1) result = result + "\n";
}
return result;
}
}
答案5
得分: 0
另一种在不生成尾部换行符的情况下构建重复矩形的方法是使用古老的Collections.nCopies
和String.join
的组合(Java 8+):
public static String getRectangle(int maxRows, int maxCols, char symbol) {
if (maxRows < 1 || maxCols < 1) {
return null;
}
String row = String.join("", Collections.nCopies(maxCols, String.valueOf(symbol)));
return String.join("\n", Collections.nCopies(maxRows, row));
}
英文:
Another way to build a repeated rectangle without the trailing new line is to use combination of ancient Collections.nCopies
and String.join
(Java 8+):
public static String getRectangle(int maxRows, int maxCols, char symbol) {
if (maxRows<1 || maxCols<1) {
return null;
}
String row = String.join("", Collection.nCopies(maxCols, String.valueOf(symbol)));
return String.join("\n", Collections.nCopies(maxRows, row));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论