gpt4 book ai didi

java - 产生随机输出 : 100 numbers from 901 to 999

转载 作者:行者123 更新时间:2023-12-01 13:09:34 24 4
gpt4 key购买 nike

我正在尝试创建两个单独的 10 列和 10 行数字输出。我知道我可以使用数字 4 到 7 进行第一个输出,使用数字 10 到 90 进行第二个输出。但是我在使用数字 901 到 999 进行第三个输出时遇到了麻烦。下面是我的 Java 代码:

import java.util.Random;

public class LabRandom
{

private static final Random rand = new Random();

public static void main(String[] args)
{

int number;

int i = 1;

while (i <= 100)
{
//number = rand.nextInt(4) + 4;
System.out.printf("%-5d", rand.nextInt(4) + 4);

if (i % 10 == 0)
{
System.out.println();
}
i++;
}
System.out.println();

i = 1;

while (i <= 100)
{
//number = rand.nextInt(4) + 4;
System.out.printf("%-5d", rand.nextInt(9)*10+10);

if (i % 10 == 0)
{
System.out.println();
}
i++;
}
System.out.println();

i = 1;

while (i <= 100)
{
//number = rand.nextInt(4) + 4;
System.out.printf("%-5d", rand.nextInt(100)*10);

if (i % 10 == 0)
{
System.out.println();
}
i++;
}

}
}

最佳答案

我无法理解你想要什么。如果您想创建 900 到 999 范围内的 100 个随机选择的数字的输出,并且每 10 个这样的数字后换行,请尝试将此循环添加到您的代码中:

i = 1;
while (i <= 100)
{
// generate a random number from 0 to 100-1,
// then add 900 to transform the range to 900 to 999
System.out.printf("%-5d", rand.nextInt(100) + 900);

if (i % 10 == 0)
{
System.out.println();
}
i++;
}

关于java - 产生随机输出 : 100 numbers from 901 to 999,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22991994/

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