0

Im making a program to take the user input which stands for how many numbers they want, and i have to show as many random numbers as they say. heres the little part i have so far:

if(selection==3)
{
    System.out.print("How many numbers would you like to see?");
    int ran=kb.nextInt();

}

(theres more code but i just cant figure out how to di this part. any help? thank you :)

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
yoyo
  • 23
  • 1
  • 1
  • 3

3 Answers3

1

I am not sure if I understood your question correctly,

  • If you are looking to generate a random number within the range of 0-n where n being the input, then try
    random.nextInt(n)
  • If you are looking to generate n number of random numbers, where n being the input, then Write a simple for loop to generate n number of random numbers.
0

Check out the Commons Math lib. There are a lot of nice methods in there to just reuse and be done with it.

Jason McD
  • 567
  • 1
  • 4
  • 12
0

I don't know what kb is, but you'll need to get the user's input from System.in. It will be a string. You'll want to do something like this (assuming you've put the user's input in a String variable called userInput):

try {
  int ran = new Random().nextInt(Integer.parseInt(userInput.trim()));
} catch (NumberFormatException e) {
  //handle non-number input here
}

Java's built-in Random number generator will get you your random numbers. What this does is trim any space characters off the input string and then parse it into an integer. You'll need to handle the case that the input is bad in the exception handler.

Tim Boudreau
  • 1,741
  • 11
  • 13