Can someone please help me code this? I know how to generate the random number a
ID: 3871226 • Letter: C
Question
Can someone please help me code this? I know how to generate the random number as stated in the top group of text, but do not understand how to code the "for" loop that is being asked.Basic Program Logic Generate a random number between 2 and 20. This will be the number of values in the first group. Display the number of values in the group with an appropriate label. In a for loop that executes exactly "number of values in the first group" number of times, generate a random number between 0 and 99, display the random number, and determine whether the number is even, odd, or zero, and increment the appropriate counter After the for loop has finished executing, display the three counters for the first group with appropriate labels. Generate another random number between 2 and 20. This will be the number of values in the second group. Display the number of values in the group with an appropriate label.
Explanation / Answer
import java.util.Random;
public class GenerateRandomNums {
public static void main(String[] args) {
// creating random
Random random = new Random();
// declaration
int min = 2, max = 20;
int evenCount = 0, oddCount = 0, zeroCount = 0;
// generating random num between 2 and 20
int noOfValues1 = random.nextInt((max - min) + 1) + min;
System.out.println("Number of values in the first group: "
+ noOfValues1);
for (int i = 0; i < noOfValues1; i++) {
// generating random nums between 0 and 99
int rand = random.nextInt(100);
// printing random num
System.out.println("Random Number " + (i + 1) + ": " + rand);
// check whether it is zero, even or odd
if (rand == 0)
zeroCount++;
else if (rand % 2 == 0)
evenCount++;
else
oddCount++;
}
// printing the three counters
System.out.println("Number of Evens:" + evenCount);
System.out.println("Number of Odds:" + oddCount);
System.out.println("Number of Zeros:" + zeroCount);
// generating random num between 2 and 20
int noOfValues2 = random.nextInt((max - min) + 1) + min;
System.out.println("Number of values in the second group: "
+ noOfValues2);
}
}
OUTPUT:
Number of values in the first group: 6
Random Number 1: 96
Random Number 2: 88
Random Number 3: 38
Random Number 4: 1
Random Number 5: 52
Random Number 6: 89
Number of Evens:4
Number of Odds:2
Number of Zeros:0
Number of values in the second group: 20