如何在 Java 中的新行上打印多个整数
How to print multiple integers on new lines in Java
我正在研究如何 打印 4 个整数的结果 在不同的行上 通过键入 只有一个 System.out.println
(我想少打字对程序员来说更好)。
预期输出:
43
1
19
13
我的代码:
class JavaApplication1 {
public static void main(String[] args) {
int a,b,c,d;
a = -5 + 8 * 6;
b = (55+9) % 9;
c = 20 + -3*5 / 8;
d = 5 + 15 / 3 * 2 - 8 % 3 ;
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
}
}
您可以使用“\n”在 Java 中创建一个新行,因此您的代码将如下所示:
System.out.println(a + "\n" + b + "\n" + c + "\n" + d);
这将在新行打印它们。
print the results of the 4 integers on separate lines by typing only once 'system.out.println'.
很简单,您需要在要打印的文本中添加一个换行符。在大多数机器上,这由特殊字符 \n
.
表示
String output = a + "\n"
+ b + "\n"
+ c + "\n"
+ d;
System.out.println(output);
然而,有 平台独立 方法,例如方法 System.lineSeparator()
(documentation) returns。喜欢:
String lineSeparator = System.lineSeparator();
String output = a + lineSeparator
+ b + lineSeparator
+ c + lineSeparator
+ d;
System.out.println(output);
或者您可以使用 System.out.printf
方法 (documentation),它代表“打印格式”。它具有创建新行的参数 %n
,内部也使用 System.lineSeparator()
。
System.out.printf("%d%n%d%n%d%n%d%n", a, b, c, d);
%d
是数字的占位符,例如 int
。它用后面列出的参数替换所有占位符。
这是该方法接受的所有参数的完整列表:Formatter-Syntax
如果您使用 Streams 和 Lambda 寻找 Java8 解决方案,那么您可以使用此代码 (documentation):
IntStream.of(a, b, c, d).forEachOrdered(System.out::println);
它首先创建一个 stream(在本例中类似于容器)保存您的变量,然后调用 print 方法 在它的每个元素上。请注意,代码本身现在 4 次 调用 print 方法,仅 而不是 一次。不过看起来挺紧凑的。
您可以查看“相当于”如下代码:
int[] values = new int[]{a, b, c, d};
for (int value : values) {
System.out.println(value);
}
您可以通过多种方式完成:
这将打印每个变量之间的变量 new-line char
:
System.out.println(a + "\n" + b + "\n" + c + "\n" + d);
您还可以使用方法引用和 Arrays
,这将从由您的 4 个变量组成的数组创建一个动态列表,并对每个变量应用 System.out::println
:
Arrays.asList(a,b,c,d).forEach(System.out::println);
或
IntStream.of(a,b,c,d).forEach(System.out::println);
用一个基本的for each loop
也可以是个办法:
for (int i : new int[]{a, b, c, d})
System.out.println(i);
打印 format
也 :
System.out.printf("%d%n%d%n%d%n%d%n",a,b,c,d)
不要只使用 "\n"
换行符。这取决于平台,如果您在 Windows 记事本上查看包含由 "\n"
分隔的行的输出,您将看不到您期望的内容。
改用这个:
System.out.printf("%s%n%s%n%s%n%s%n", a, b, c, d);
或者:
IntStream.of(a, b, c, d).forEach(System.out::println);
您将需要使用换行符。在 java 中是 \n
要使用您拥有的打印它们,它将是:
System.out.println(a + "\n" + b + "\n" + c + "\n" + d);
使用System.out.format
,您可以使用%n
格式输出系统相关的行分隔符;您还可以向格式字符串添加更多数字格式。
System.out.format("%d%n%d%n%d%n%d%n", a, b, c, d);
您也可以选择使用 System.out.printf("%d%n%d%n%d%n%d%n",a,b,c,d)
这满足了您声明的对 System.out.println
的单次调用的要求,使用了独立于平台的行分隔符,并避免重复键入相同的连接操作:
System.out.println(IntStream.of(a,b,c,d).mapToObj(Integer::toString)
.collect(Collectors.joining(System.lineSeparator())));
这是一个更通用的版本,适用于任何对象,而不仅仅是整数:
System.out.println(Stream.of(a,b,c,d).map(Object::toString)
.collect(Collectors.joining(System.lineSeparator())));
java中有不同的打印语句
1.println
System.out.println(1+"\n"+2+"\n"+3+"\n"+4);
2.print
System.out.print("\n"+1);
System.out.print("\n"+2);
System.out.print("\n"+3);
System.out.print("\n"+4);
3.printf
System.out.printf("%d%n%d%n%d%n%d%n",1,2,3,4);
其中 %d 和 %n 称为格式说明符,它们指定将在 printf 方法中使用的数据类型。
谢谢。
我正在研究如何 打印 4 个整数的结果 在不同的行上 通过键入 只有一个 System.out.println
(我想少打字对程序员来说更好)。
预期输出:
43
1
19
13
我的代码:
class JavaApplication1 {
public static void main(String[] args) {
int a,b,c,d;
a = -5 + 8 * 6;
b = (55+9) % 9;
c = 20 + -3*5 / 8;
d = 5 + 15 / 3 * 2 - 8 % 3 ;
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
}
}
您可以使用“\n”在 Java 中创建一个新行,因此您的代码将如下所示:
System.out.println(a + "\n" + b + "\n" + c + "\n" + d);
这将在新行打印它们。
print the results of the 4 integers on separate lines by typing only once 'system.out.println'.
很简单,您需要在要打印的文本中添加一个换行符。在大多数机器上,这由特殊字符 \n
.
String output = a + "\n"
+ b + "\n"
+ c + "\n"
+ d;
System.out.println(output);
然而,有 平台独立 方法,例如方法 System.lineSeparator()
(documentation) returns。喜欢:
String lineSeparator = System.lineSeparator();
String output = a + lineSeparator
+ b + lineSeparator
+ c + lineSeparator
+ d;
System.out.println(output);
或者您可以使用 System.out.printf
方法 (documentation),它代表“打印格式”。它具有创建新行的参数 %n
,内部也使用 System.lineSeparator()
。
System.out.printf("%d%n%d%n%d%n%d%n", a, b, c, d);
%d
是数字的占位符,例如 int
。它用后面列出的参数替换所有占位符。
这是该方法接受的所有参数的完整列表:Formatter-Syntax
如果您使用 Streams 和 Lambda 寻找 Java8 解决方案,那么您可以使用此代码 (documentation):
IntStream.of(a, b, c, d).forEachOrdered(System.out::println);
它首先创建一个 stream(在本例中类似于容器)保存您的变量,然后调用 print 方法 在它的每个元素上。请注意,代码本身现在 4 次 调用 print 方法,仅 而不是 一次。不过看起来挺紧凑的。
您可以查看“相当于”如下代码:
int[] values = new int[]{a, b, c, d};
for (int value : values) {
System.out.println(value);
}
您可以通过多种方式完成:
这将打印每个变量之间的变量 new-line char
:
System.out.println(a + "\n" + b + "\n" + c + "\n" + d);
您还可以使用方法引用和 Arrays
,这将从由您的 4 个变量组成的数组创建一个动态列表,并对每个变量应用 System.out::println
:
Arrays.asList(a,b,c,d).forEach(System.out::println);
或
IntStream.of(a,b,c,d).forEach(System.out::println);
用一个基本的for each loop
也可以是个办法:
for (int i : new int[]{a, b, c, d})
System.out.println(i);
打印 format
也 :
System.out.printf("%d%n%d%n%d%n%d%n",a,b,c,d)
不要只使用 "\n"
换行符。这取决于平台,如果您在 Windows 记事本上查看包含由 "\n"
分隔的行的输出,您将看不到您期望的内容。
改用这个:
System.out.printf("%s%n%s%n%s%n%s%n", a, b, c, d);
或者:
IntStream.of(a, b, c, d).forEach(System.out::println);
您将需要使用换行符。在 java 中是 \n
要使用您拥有的打印它们,它将是:
System.out.println(a + "\n" + b + "\n" + c + "\n" + d);
使用System.out.format
,您可以使用%n
格式输出系统相关的行分隔符;您还可以向格式字符串添加更多数字格式。
System.out.format("%d%n%d%n%d%n%d%n", a, b, c, d);
您也可以选择使用 System.out.printf("%d%n%d%n%d%n%d%n",a,b,c,d)
这满足了您声明的对 System.out.println
的单次调用的要求,使用了独立于平台的行分隔符,并避免重复键入相同的连接操作:
System.out.println(IntStream.of(a,b,c,d).mapToObj(Integer::toString)
.collect(Collectors.joining(System.lineSeparator())));
这是一个更通用的版本,适用于任何对象,而不仅仅是整数:
System.out.println(Stream.of(a,b,c,d).map(Object::toString)
.collect(Collectors.joining(System.lineSeparator())));
java中有不同的打印语句
1.println
System.out.println(1+"\n"+2+"\n"+3+"\n"+4);
2.print
System.out.print("\n"+1);
System.out.print("\n"+2);
System.out.print("\n"+3);
System.out.print("\n"+4);
3.printf
System.out.printf("%d%n%d%n%d%n%d%n",1,2,3,4);
其中 %d 和 %n 称为格式说明符,它们指定将在 printf 方法中使用的数据类型。 谢谢。