Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java与随机方法_Java_Random - Fatal编程技术网

Java与随机方法

Java与随机方法,java,random,Java,Random,我想用* 由于闪烁,我决定使用重复5000次的“for”,并且我希望每行中的Max*为25,但我的代码不起作用。有人有什么建议吗?我的代码是 void test() { Random rnd = new Random(); for (int i = 0; i < 5000; i++) { rnd = nextInt(25); for (int j = 0; j < rnd; j++) { System.ou

我想用*
由于闪烁,我决定使用重复5000次的“for”,并且我希望每行中的Max*为25,但我的代码不起作用。有人有什么建议吗?我的代码是

 void test() {

    Random rnd = new Random();
    for (int i = 0; i < 5000; i++) {
        rnd = nextInt(25);

        for (int j = 0; j < rnd; j++) {
            System.out.print(" * ");

        }

    }

}
void测试(){
随机rnd=新随机();
对于(int i=0;i<5000;i++){
rnd=nextInt(25);
对于(int j=0;j
您对
nextInt
的分配没有正确完成。将
rnd.nextInt(25)
分配给一个单独的
int
变量,如下所示:

Random rnd = new Random();
for (int i = 0; i < 5000; i++) {

    int rndInt = rnd.nextInt(25);

    for (int j = 0; j < rndInt; j++) {
        System.out.print(" * ");

    }

}
Random rnd=new Random();
对于(int i=0;i<5000;i++){
int rndInt=rnd.nextInt(25);
对于(int j=0;j
Random rnd=new Random();
对于(int i=0;i<5000;i++){
int rndInt=rnd.nextInt(25);
对于(int j=0;j
闪烁是什么意思?你的问题不是很清楚。首先,您的代码有明显的语法错误。您可以从搜索如何在Java中使用Random类开始。@我在输出中的意思是在每行显示随机星,例如,如果发生5000次,它将闪烁。@Davidorenzomarino我在输出中的意思是在每行显示随机星,例如,如果发生5000次,会的flashing@Graysontnx为您的解决方案!
Random rnd = new Random();
for (int i = 0; i < 5000; i++) {

    int rndInt = rnd.nextInt(25);

    for (int j = 0; j < rndInt; j++) {
        System.out.print(" * ");

    }
    System.out.println();  // newline

}