What I'm trying to do is use a while or do-while loop to generate a set of 20 different random numbers, then display the average, number of positive numbers and number of negative numbers. I'm stumped on generating the set of numbers to use, but I think I have everything else done right.
int positive = 0;
int negative = 0;
int min = 0;
int max = 0;
int count = 0;
int total= 0;
int average;
Random r = new Random();
int number;
while (count >= 66) {
number = r.nextInt(150)-67;
total += number;
count++;
if (number > 0) {
positive++;
}
else if (number < 0) {
negative++;
}
}
average = (double) total / count;
System.out.println("The number of positives is " + positive + " ");
System.out.println("The number of negatives is " + negative + " ");
System.out.printf("The average is: " + average);
Aucun commentaire:
Enregistrer un commentaire