从Java中删除结果末尾的空行

huangapple go评论62阅读模式
英文:

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(&quot;You must implement this method.&quot;);
    if (maxRows&lt;1 || maxCols&lt;1) { 
	    return null; 
	} else { 
		String result = &quot;&quot;;
		for (int i = 0; i &lt; maxRows; i++) { 
		    for (int j = 0; j &lt; maxCols; j++) { 
				result = result + symbol;
            } 
			result = result + &quot;\n&quot;;
        } 
		return result;	 
	}
}

答案1

得分: 3

在这里添加一个条件:

if (i != maxRows - 1) {
    result = result + "\n";
}

虽然我建议使用 StringBuilder 而不是字符串来存储结果。

英文:

Add a condition here:

if (i != maxRows - 1) {
    result = result + &quot;\n&quot;;
}

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&lt;1 || maxCols&lt;1) {
		return null;
	} else {
		String row = String.valueOf(symbol).repeat(maxCols);  // create a row
		String columns = (row + &quot;\n&quot;).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&lt;1 || maxCols&lt;1) {
		return null;
	} else {
		StringBuilder row = new StringBuilder();
		StringBuilder column = new StringBuilder();
		for (int i = 0; i &lt; maxCols; i++) {        // rows
			row.append(symbol);
		}
		for (int i = 0; i &lt; maxRows - 1; i++) {    // n-1 columns
			column.append(row).append(&quot;\n&quot;);
		}
		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(&quot;You must implement this method.&quot;);
        if (maxRows &lt; 1 || maxCols &lt; 1) {
            return null;
        } else {
            String result = &quot;&quot;;
            for (int i = 0; i &lt; maxRows; i++) {
                for (int j = 0; j &lt; maxCols; j++) {
                    result = result + symbol;
                }
                if (i != maxRows - 1) result = result + &quot;\n&quot;;
            }
            return result;
        }
    }

答案5

得分: 0

另一种在不生成尾部换行符的情况下构建重复矩形的方法是使用古老的Collections.nCopiesString.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&lt;1 || maxCols&lt;1) {
        return null;
    }
    String row = String.join(&quot;&quot;, Collection.nCopies(maxCols, String.valueOf(symbol)));
    return String.join(&quot;\n&quot;, Collections.nCopies(maxRows, row));                                
}

huangapple
  • 本文由 发表于 2020年9月30日 05:25:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/64127863.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定