vendredi 6 novembre 2020

Java :Generate random numbers between minimum and a maximum values

This is the exercise I am stuck with :

Ask user for minimum, maximum (inclusive) and how many (count) random numbers user wants to generate. Makes sure minimum is not larger than maximum and count is not negative. Display error message if inputs are invalid. If all is fine then generate the random numbers and print them out, comma-separated on a single line.

I wrote the code in many ways. I can never get the for loop to print the Random numbers. Please help!

package randnumsmany;

import java.util.Scanner;

public class RandNumsMany {

    public static double getRandomNumber(int min, int max) {
    return (int) ((Math.random() * (max - min)) + min);
}

public static void main(String[] args) {
   Scanner input = new Scanner(System.in);
    System.out.println("Enter minimum :");
    int min = input.nextInt();
    
    System.out.println("Enter maximum :");
    int max = input.nextInt();
    
    System.out.println("Enter number generated :");
    int num = input.nextInt();
    
    int[] rand = new int[num];
    
    for (int i = 0; i == num-1; i++) {
    rand[i] = (int)getRandomNumber(min,max);
    }
    for (int i = 0; i == num-1; i++) {
    System.out.println(rand[i]);
    }
    
    
    System.out.println("How many generated: " + num);
    
}

}




Aucun commentaire:

Enregistrer un commentaire