如何删除每行末尾的尾随空格?
How to remove trailing whitespaces at the end of each line?
我正在 Dcoder 中尝试挑战,我的答案与预期输出相同,但我认为问题出在给定情况下,以删除每行末尾的尾随空格。
说清楚,就是这个问题:
You need to print this pattern up to N
, e.g. N = 3
Expected Output:
1
1 2
1 2 3
Do not leave trailing spaces at the end of each line!
这是我的代码:
String sp = " ";
for (int rows = 1; rows <= range; rows++) { //rows
for (int cols = 1; cols <= rows; cols++) {
System.out.print(Integer.toString(cols) + sp);
}
System.out.println(sp.trim());
}
我尝试连接 Integer.toString(cols)
和 sp
,然后另一个 sp.trim()
输出也是相同的,但挑战并没有说它是正确的,为什么会这样?谁能解释一下,或者我的代码有问题吗?
您在第二个 for
循环中的每个数字后附加了空格 sp
。这就是打印行时尾随空格的原因。
您有多种选择,例如使用 StringBuilder
和 .append()
值,然后打印 toString().trim()
,但这是您的代码的一个非常简单的扩展,我只是硬编码range
来自 4
:
public static void main(String[] args) {
String sp = " ";
for (int rows = 1; rows <= 4; rows++) {
for (int cols = 1; cols <= rows; cols++) {
// here you need to find out if it is the last number to be printed
if (cols == rows) {
// if it is, just print that number
System.out.print(Integer.toString(cols));
} else {
// otherwise print the number and the whitespace
System.out.print(Integer.toString(cols) + sp);
}
}
// force a linebreak
System.out.println();
}
}
输出
1
1 2
1 2 3
1 2 3 4
备选方案:
public static void main(String[] args) {
for (int rows = 1; rows <= 4; rows++) {
StringBuilder lineBuilder = new StringBuilder();
for (int cols = 1; cols <= rows; cols++) {
if (cols == rows) {
lineBuilder.append(Integer.toString(cols));
} else {
lineBuilder.append(Integer.toString(cols)).append(" ");
}
}
System.out.println(lineBuilder.toString());
}
}
String sp = " ";
for (int rows = 1; rows <= 3; rows++){ //rows
String pattern ="";
for (int cols = 1; cols <= rows; cols++)
pattern += Integer.toString(cols) + sp;
System.out.println(pattern.trim());
}
好了
int range = 3;
for(int rows = 1; rows <= range; rows++ ) {
for(int cols = 1; cols <= rows; cols++ ) {
if (cols == rows) {
System.out.println(Integer.toString(cols));
} else {
System.out.print(Integer.toString(cols) + " ");
}
}
}
从 Java 8 开始,您可以使用 Collectors.joining
方法将字符串与空格 连接起来 :
int n = 5;
IntStream.rangeClosed(1, n)
// row of numbers
.mapToObj(i -> IntStream.rangeClosed(1, i)
// number as string
.mapToObj(String::valueOf)
// join strings into one line
// with spaces in between
.collect(Collectors.joining(" ")))
// output line by line
.forEach(System.out::println);
或者你可以使用String.join
方法,效果相同:
int n = 5;
for (int i = 1; i <= n; i++) {
// row of numbers
String[] row = new String[i];
for (int j = 0; j < i; j++) {
// number as string
row[j] = String.valueOf(j + 1);
}
// join an array of strings into
// one line with spaces in between
System.out.println(String.join(" ", row));
}
输出:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
另请参阅:
我正在 Dcoder 中尝试挑战,我的答案与预期输出相同,但我认为问题出在给定情况下,以删除每行末尾的尾随空格。
说清楚,就是这个问题:
You need to print this pattern up to
N
, e.g.N = 3
Expected Output:
1 1 2 1 2 3
Do not leave trailing spaces at the end of each line!
这是我的代码:
String sp = " ";
for (int rows = 1; rows <= range; rows++) { //rows
for (int cols = 1; cols <= rows; cols++) {
System.out.print(Integer.toString(cols) + sp);
}
System.out.println(sp.trim());
}
我尝试连接 Integer.toString(cols)
和 sp
,然后另一个 sp.trim()
输出也是相同的,但挑战并没有说它是正确的,为什么会这样?谁能解释一下,或者我的代码有问题吗?
您在第二个 for
循环中的每个数字后附加了空格 sp
。这就是打印行时尾随空格的原因。
您有多种选择,例如使用 StringBuilder
和 .append()
值,然后打印 toString().trim()
,但这是您的代码的一个非常简单的扩展,我只是硬编码range
来自 4
:
public static void main(String[] args) {
String sp = " ";
for (int rows = 1; rows <= 4; rows++) {
for (int cols = 1; cols <= rows; cols++) {
// here you need to find out if it is the last number to be printed
if (cols == rows) {
// if it is, just print that number
System.out.print(Integer.toString(cols));
} else {
// otherwise print the number and the whitespace
System.out.print(Integer.toString(cols) + sp);
}
}
// force a linebreak
System.out.println();
}
}
输出
1
1 2
1 2 3
1 2 3 4
备选方案:
public static void main(String[] args) {
for (int rows = 1; rows <= 4; rows++) {
StringBuilder lineBuilder = new StringBuilder();
for (int cols = 1; cols <= rows; cols++) {
if (cols == rows) {
lineBuilder.append(Integer.toString(cols));
} else {
lineBuilder.append(Integer.toString(cols)).append(" ");
}
}
System.out.println(lineBuilder.toString());
}
}
String sp = " ";
for (int rows = 1; rows <= 3; rows++){ //rows
String pattern ="";
for (int cols = 1; cols <= rows; cols++)
pattern += Integer.toString(cols) + sp;
System.out.println(pattern.trim());
}
好了
int range = 3;
for(int rows = 1; rows <= range; rows++ ) {
for(int cols = 1; cols <= rows; cols++ ) {
if (cols == rows) {
System.out.println(Integer.toString(cols));
} else {
System.out.print(Integer.toString(cols) + " ");
}
}
}
从 Java 8 开始,您可以使用 Collectors.joining
方法将字符串与空格 连接起来 :
int n = 5;
IntStream.rangeClosed(1, n)
// row of numbers
.mapToObj(i -> IntStream.rangeClosed(1, i)
// number as string
.mapToObj(String::valueOf)
// join strings into one line
// with spaces in between
.collect(Collectors.joining(" ")))
// output line by line
.forEach(System.out::println);
或者你可以使用String.join
方法,效果相同:
int n = 5;
for (int i = 1; i <= n; i++) {
// row of numbers
String[] row = new String[i];
for (int j = 0; j < i; j++) {
// number as string
row[j] = String.valueOf(j + 1);
}
// join an array of strings into
// one line with spaces in between
System.out.println(String.join(" ", row));
}
输出:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
另请参阅: