0

While writing a code for printing out random numbers ranging from 0 to 99, I had faced some difficulties. I tried to change the condition in the for statement to e<100, however, it had occurred an error. Where should I put the conditions in order for my output to show numbers between 0 to 99?

public class EvensOdds {

public static void printArray(int[] ear) {
    
    System.out.println("ODD NUMBERS : ");
    for (int e = 0; e<ear.length ; e ++) {
        ear[e] = (int)(Math.random()* 10);
            if(ear[e]%2!=0)
            System.out.print(ear[e] + "  ");
    }
    System.out.println("\n" + "EVEN NUMBERS : ");
    for (int e = 0; e<ear.length ; e ++) {
        ear[e] = (int)(Math.random()* 10);
            if(ear[e]%2==0)
            System.out.print(ear[e] + "  ");
    }
    System.out.println();
  }

public static void main(String[] args) {
    int[] numberArray = new int[25];
    printArray(numberArray);

}

}
idklife
  • 11
  • 4
  • `Math.random()` Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. By multiplying by 10 you cover the range [0,10). To extend it to the range [0,100) multiply it by 100. – Eritrean Jul 22 '20 at 13:38
  • Please look at : https://stackoverflow.com/questions/363681/how-do-i-generate-random-integers-within-a-specific-range-in-java – ggr Jul 22 '20 at 13:40
  • Why are you using an array? Especially considering you are overwriting it with the even numbers. Just use a simple int variable and pass the desired count to the method. – WJS Jul 22 '20 at 14:59

1 Answers1

1

In Java 8, below creates an array with size 25, with random entries between 0,99

    Random random = new Random();
    int[] array = random.ints(25, 0, 100).toArray();
Maifee Ul Asad
  • 3,992
  • 6
  • 38
  • 86
  • 1
    You should correct your answer and change 99 to 100 as the OP wanted to include 99. The way you wrote it only numbers `0 <= x <= 98` will be considered. – WJS Jul 22 '20 at 16:38