Reputation: 11
Here is my assignment:
Write a program to read a list of nonnegative integers and to display the largest integer, the smallest integer, and the average of all the integers. The user
indicates the end of the input by entering a negative sentinel value that is not
used in finding the largest, smallest, and average values. The average should
be a value of type double
so it will be computed with a fractional part.
I've gotten different parts to work with different methods: Method A makes the maximum and minimum correct and the sum wrong and Method B makes the sum and maximum correct and the minimum wrong. The following code demonstrates Method B. Some variables are commented out:
public class testthis2
{
public static void main(String[] args) {
System.out.println("Enter Numbers of Nonnegative Integers.");
System.out.println("When complete, enter -1 to end input values.");
Scanner keyboard = new Scanner(System.in);
//int max = keyboard.nextInt();
int max = 0;
int min = max; //The max and min so far are the first score.
//int next = keyboard.nextInt();
int count = 0;
int sum = 0;
boolean areMore = true;
boolean run_it = false; //run it if its true
//if (max <= -1) {
// System.out.println("Thanks for playing!");
// run_it = false;
//}
// else
// run_it = true;
while(areMore) //always true
{
int next = keyboard.nextInt();
//int max = 0;
// min = max;
if (next < 0) { //if -1 is entered end loop.
areMore = false;
run_it = false;
break;
}
else //run this badboy
if(next >= max)
max = next;
else if(next <= min)
min = next;
run_it = true;
sum += next;
count++;
}
if (run_it = true)
System.out.println("The highest score is " + max);
System.out.println("The lowest score is " + min);
System.out.println("count " + count);
System.out.println("sum " + sum);
System.out.println("average " + (double)(sum/count));
System.out.println("Thanks for playing!");
}
}
When I run this test, the maximum, sum, count, and average are all correct. However, the minimum is wrong, because 0 was clearly not entered. Here's an example test-run:
When complete, enter -1 to end input values.
37
25
30
20
11
14
-1
The highest score is 37
The lowest score is 0
count 6
sum 137
average 22.0
Thanks for playing!
Any help would be greatly appreciated. Thanks!
Upvotes: 1
Views: 2847
Reputation: 31648
There are 2 problems with the code:
min
to 0 so it never gets updated because it will always be <= any valid number you enter. Try initializing it to Integer.MAX_VALUE
. conversely also initialize max
to Integer.MIN_VALUE
(double)(sum/count)
will first do integer division which truncates the value THEN gets cast to double do this instead ((double)(sum )/count)
or optionally make the type of sum
a double.Upvotes: 1
Reputation:
The smallest iteger is always 0 because there is no nonnegative integer that is less then 0 :)
if(next <= min) // for nonnegative integer this expression will return true only for 0
min = next;
So try to initialize the "min" variable as Integer.MAX_VALUE. I believe it will help you.
Upvotes: 1
Reputation: 62062
Looks like you initialize min
and max
both to 0.
Then the only code that will ever change min
or max
is based on user input. If the input value (next
) is >= max
, max
will change to that value. This should happen on the first input.
The problem is you try setting min
the same way. if (next <= min)
, but min
was initialized to 0, so next
can only be <= min
if next
is less than 0.
You need to initialize max
and min
on the user's first input. They should both start equal to the user's first
input before you compare future inputs to their value.
Upvotes: 0