用一个新行循环而不是多个 JAVA
loop with one new line not multiple JAVA
Java 这里是新手,
所以我正在尝试编写一个程序,可以设置 hello worlds 的数量,以及它后面的感叹号数量,使用命令行参数输入这些值。我已经以某种方式完成了,但是输出格式是错误的。
期望的结果
“你好世界!!!!
"Hello World !!!!"
尝试 1
“你好世界!
!
!
!”(继续向下)
我得到了什么,尝试 2
"Hello World !!!!Hello World!!!!Hello World!!!!"
我的尝试 1 代码
public class NHelloWorldWE {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//
int n = Integer.parseInt(args[0]);
int e = Integer.parseInt(args[1]);
for (int a = 1; a <= n; a = a + 1) {
System.out.print("Hello World");
for (int b = 1; b <= e; b = b + 1) {
System.out.print("!");
System.out.println(" ");
}
}
}
}
我的尝试 2 代码
public class NHelloWorldWE {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//
int n = Integer.parseInt(args[0]);
int e = Integer.parseInt(args[1]);
for (int a = 1; a <= n; a = a + 1) {
System.out.print("Hello World");
for (int b = 1; b <= e; b = b + 1) {
System.out.print("!");
}
}
}
}
您需要换行打印:
System.out.println("Hello World");
像这样:
public static void main(String[] args) {
//
int n = Integer.parseInt(args[0]);
int e = Integer.parseInt(args[1]);
for (int a = 1; a <= n; a = a + 1) {
System.out.print("Hello World");
for (int b = 1; b <= e; b = b + 1) {
System.out.print("!");
}
System.out.println();
}
}
这个会给你一个类似的结果,但每次迭代都会少 1 个感叹号(我相信这就是你想要做的)
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
int e = Integer.parseInt(args[1]);
for (int a = 1; a <= n; a++) {
System.out.print("Hello World");
for (int b = 1; b <= e; b++) {
System.out.print("!");
}
e--;
System.out.println();
}
}
这是您的解决方案的一个版本:
public class NHelloWorldWE {
public static void main(String... args) {
int n = Integer.parseInt(args[0]);
int e = Integer.parseInt(args[1]);
for (int a = 0; a < n; a++) {
System.out.print("Hello World");
for (int b = 0; b < e; b++) {
System.out.print("!");
}
System.out.println();
}
}
}
这是一个使用流的例子:
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class NHelloWorldWE {
public static void main(String... args) {
int n = Integer.parseInt(args[0]);
int e = Integer.parseInt(args[1]);
final String hello = "Hello World" + Stream.generate(() -> "!")
.limit(e)
.collect(Collectors.joining());
Stream.generate(() -> hello)
.limit(n)
.forEach(System.out::println);
}
}
Java 这里是新手,
所以我正在尝试编写一个程序,可以设置 hello worlds 的数量,以及它后面的感叹号数量,使用命令行参数输入这些值。我已经以某种方式完成了,但是输出格式是错误的。
期望的结果
“你好世界!!!!
"Hello World !!!!"
尝试 1
“你好世界!
!
!
!”(继续向下)
我得到了什么,尝试 2
"Hello World !!!!Hello World!!!!Hello World!!!!"
我的尝试 1 代码
public class NHelloWorldWE {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//
int n = Integer.parseInt(args[0]);
int e = Integer.parseInt(args[1]);
for (int a = 1; a <= n; a = a + 1) {
System.out.print("Hello World");
for (int b = 1; b <= e; b = b + 1) {
System.out.print("!");
System.out.println(" ");
}
}
}
}
我的尝试 2 代码
public class NHelloWorldWE {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//
int n = Integer.parseInt(args[0]);
int e = Integer.parseInt(args[1]);
for (int a = 1; a <= n; a = a + 1) {
System.out.print("Hello World");
for (int b = 1; b <= e; b = b + 1) {
System.out.print("!");
}
}
}
}
您需要换行打印:
System.out.println("Hello World");
像这样:
public static void main(String[] args) {
//
int n = Integer.parseInt(args[0]);
int e = Integer.parseInt(args[1]);
for (int a = 1; a <= n; a = a + 1) {
System.out.print("Hello World");
for (int b = 1; b <= e; b = b + 1) {
System.out.print("!");
}
System.out.println();
}
}
这个会给你一个类似的结果,但每次迭代都会少 1 个感叹号(我相信这就是你想要做的)
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
int e = Integer.parseInt(args[1]);
for (int a = 1; a <= n; a++) {
System.out.print("Hello World");
for (int b = 1; b <= e; b++) {
System.out.print("!");
}
e--;
System.out.println();
}
}
这是您的解决方案的一个版本:
public class NHelloWorldWE {
public static void main(String... args) {
int n = Integer.parseInt(args[0]);
int e = Integer.parseInt(args[1]);
for (int a = 0; a < n; a++) {
System.out.print("Hello World");
for (int b = 0; b < e; b++) {
System.out.print("!");
}
System.out.println();
}
}
}
这是一个使用流的例子:
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class NHelloWorldWE {
public static void main(String... args) {
int n = Integer.parseInt(args[0]);
int e = Integer.parseInt(args[1]);
final String hello = "Hello World" + Stream.generate(() -> "!")
.limit(e)
.collect(Collectors.joining());
Stream.generate(() -> hello)
.limit(n)
.forEach(System.out::println);
}
}