Home > database >  Generating 100 random numbers and comparing them to user inpuit
Generating 100 random numbers and comparing them to user inpuit

Time:10-23

So i am having a heck of a time with this problem. I need to Ask a user for a value and then generate 100 random numbers and count the number of odd numbers above the user entered value. Ia m at a lost and just want to toss the computer in the street! Please help! Thank You! I have posted what i have so far below.

import java.util.Scanner;

public class Test {

    public static void main(String[] args) {
    
         Scanner input = new Scanner(System.in);
            System.out.println("Enter a value between 30 and 70: ");
        
           while (true) { 
            int user = input.nextInt();        
            if (user > 70)
               System.out.println("Value higher than 70, Please re-enter value: ");
            else if (user < 30)
              System.out.println("Value lower than 30, Please re-enter a value: ");
            else
              System.out.println("The value entered by the user is "   user);

     }
 }

CodePudding user response:

After getting a value from the user, use a for loop that iterates a hundred times, and at each round, you generate a random number (choose the bounds) then add the condition and count

Scanner input = new Scanner(System.in);
System.out.println("Enter a value between 30 and 70: ");

int user;
while (true) {
    user = input.nextInt();
    if (user > 70) {
        System.out.println("Value higher than 70, Please re-enter value: ");
    } else if (user < 30) {
        System.out.println("Value lower than 30, Please re-enter a value: ");
    } else {
        System.out.println("The value entered by the user is "   user);
        break;
    }
}
int count = 0;
Random r = new Random();
for (int i = 0; i < 100; i  ) {
    int rnd = r.nextInt(100);
    if (rnd > user && rnd % 2 == 1) {
        count  ;
    }
}
System.out.println("There is "   count   " values odd numbers above your value");

CodePudding user response:

I would suggest filling an array with random numbers and then looping through it. Here are some links that might help you:

Generating random numbers

Generating an array filled with random numbers

Looping through arrays

Check if a number is odd

  •  Tags:  
  • java
  • Related