M.ds M.ds - 5 months ago 11
Java Question

Java and Random method

I want to create something like Flashing Light with *.

for being Flashing I decided to use "For" that Repeats 5000 times , also I want the Max * in each line be 25 but my code is not working . Does any body have any suggestion ? my code is

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(" * ");

}

}

}

Answer
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

}
Comments