mercredi 1 août 2018

method that return only odd numbers (int) in java

So, I am tring to create a method that return random numbers within range (1-43), but only odd numbers. I used do-while statment, but the program keeps returning many kinds of numbers - even and odd alike. can you please tell me whats wrong with it, so it will return only the requested type of number (depended on: % = 0/1)?

public static String generateRandomOddNumbersWithinRange(){

    int randomNum = ThreadLocalRandom.current().nextInt(1, 43 + 1);

    do {
        return Integer.toString(randomNum);

    } while (randomNum % 2 == 1);
}




Aucun commentaire:

Enregistrer un commentaire