gpt4 book ai didi

java - 每个 for 循环都打印在一个新行上,即使它不应该

转载 作者:行者123 更新时间:2023-12-02 01:20:05 27 4
gpt4 key购买 nike

我正在尝试为 Minecraft 制作一个 3 字母名称生成器,并计划与我不存在的 friend 分享它。

package bored;
import java.util.Random;
public class Yay {

public static void main(String[] args) {
String[] arr={"1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "_"};
int i;
int f;
for (f=0; f<10; f++) {
for (i=0; i<3; i++) {
Random r=new Random();
int randomNumber=r.nextInt(arr.length);
System.out.print(arr[randomNumber]);
System.out.println("");
}
}
}
}

它应该打印类似“3ab”“9dl”的内容但它打印的是

3

一个

b

9

l

最佳答案

您当前正在内部 for 循环内调用换行打印语句。您可以从此更改代码

for (f=0; f<10; f++) {
for (i=0; i<3; i++) {
Random r=new Random();
int randomNumber=r.nextInt(arr.length);
System.out.print(arr[randomNumber]);
System.out.println("");
}
}

到此

for (f=0; f<10; f++) {
for (i=0; i<3; i++) {
Random r=new Random();
int randomNumber=r.nextInt(arr.length);
System.out.print(arr[randomNumber]);
}
System.out.println();
}

关于java - 每个 for 循环都打印在一个新行上,即使它不应该,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57878961/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com