dimanche 28 avril 2019

Generate list of random numbers starting with x

so I was asked to generate a list of random numbers from 1 to 100. Then I was asked to print a message at each random number that was divisible by 7, that was okay too.

So now my issue is that this list has to start with the number 1 and has then to be continued with random numbers. Addiotionally, I wand to print a certain text every fifth line.

Questions: 1) How do I start my list with the number 1 and still keep the rest random? 2) How do I print a message every fifth line?

Best regards, Djabone

I have been searching for 2 hours and only found results for python and other languages. I was not able to find the right answer.

import java.util.Random;

public class rannumb {

public static void main(String[] args) {

    Random rnd = new Random();
    int number;

    for(int i = 1; i<=100; i++) {
        if (i%7==0) {
            System.out.println(i+ " : Lucky number!");
        }

        number = rnd.nextInt(100);
        System.out.println(number); 

    }
}

}

The output I get is :

  • 3, 69, 75, 83, 96, 47, 7 : Lucky number!, 56, 30, 98, 6, 66, 97, 63, 14 : Lucky number!

The output I expect to get is:

  • 1, 3, 69, 75, 83 : message, 96, 47, 7 : Lucky number!, 56, 30 : message, 98, 6, 66, 97, 63, 14 : Lucky number!



Aucun commentaire:

Enregistrer un commentaire