Reputation: 31
If you could give me some help with an assignment I have for my Java class, I'd greatly appreciate it. The prompt for the question is:
Write a program to read a list of non-negative 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 that it is computed with a fractional part.
The problem I'm experiencing with my code is that when run, the loop does not finish unless the first value entered is negative, in which case it returns:
The maximum number entered was: 0 The minimum number entered was: 0 The average of the numbers entered was: NaN
Please help! Thanks. -Sam
code:
package blah;
import java.util.Scanner;
public class blahblah
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.println ("Please enter a list of positive integers.");
System.out.println ("Please enter a negative integer when finished.");
int in = 0;
int max = 0;
int min = 0;
int sum = 0;
int count = 0;
in = keyboard.nextInt();
while (in>=0)
{
if (in > max) {
in = max;
}
if (in < min) {
in = min;
}
sum += in;
count++;
if (in < 0) {
break;
}
}
System.out.println("The maximum number entered was: " + max);
System.out.println("The minimum number entered was: " + min);
System.out.println("The average of the numbers entered was: " + (double)sum/count);
}
}
Upvotes: 3
Views: 12772
Reputation: 1
Scanner input =new Scanner(System.in);
double a= 0;
int i=0;
double sum=0;
double average=0;
double smallest=0;
double largest=0;
double range=0;
while(a>=0){
System.out.print("Enter the value : ");
a=input.nextDouble();
if(a<0){
break;
}
i++;
sum+=a;
average=sum/i;
if(smallest==0){
smallest=a;
}
if(smallest>a){
smallest=a;
}
if(largest<a){
largest=a;
}
range=largest-smallest;
}
System.out.print("\n The average of all the values is : "+average);
System.out.print("\n The smallest of the values is : "+smallest);
System.out.print("\n The largest of the values is : "+largest);
System.out.print("\n The range of the values is : "+range);
Upvotes: 0
Reputation: 425033
Move the read of input inside the loop, and break on negative:
while (true) {
in = keyboard.nextInt();
if (in < 0) break;
// rest of loop
}
A better appraoch would be to use a for
loop, which nicely bundles up all the loop-related logic:
for (int in = keyboard.nextInt(); in >= 0; in = keyboard.nextInt()) {
// your current loop code
}
Separating out the iteration code makes it clear what code is the iteration code and leaves the loop code to be entirely dedicated to the program's task, making ing easier to read and understand
This also means you don't need to declare int in
, and it is good practice to reduce the scope of one's variables as much as possible - in this case in
would only exists within the loop, which is the only place it's used/needed.
Upvotes: 1
Reputation: 3804
You are putting the values in same variable "in"
in=max;
It should be other way
max=in;
Upvotes: 0
Reputation: 11979
You need to read the nextInt again inside you loop:
while (in>=0)
{
if (in>max){
max=in;
}
if (in<min){
min=in;
}
sum += in;
count++;
in = keyboard.nextInt();
//Check not needed here, handled by while loop
//if (in<0){
// break;
//}
}
Edit from comment: your assignment was going the wrong direction, so you were setting input equal to min/max instead of setting min/max equal to input
Upvotes: 3
Reputation: 12169
You statement to read the values is not inside the while loop, so it is only reading the first entry:
in = keyboard.nextInt ();
while (in>=0)
{
}
Change to:
in = keyboard.nextInt ();
while (in>=0)
{
... stuff ...
in = keyboard.nextInt ();
}
Upvotes: 1
Reputation: 1661
Change your code to
in = keyboard.nextInt ();
while (in>=0){
if (in>max){
in=max;
}
if (in<min){
in=min;
}
sum += in;
count++;
in = keyboard.nextInt ();
}
As you can see i have added in = keyboard.nextInt ();
to enable getting more values from the user
Upvotes: 0
Reputation: 16234
You have to reread input from user. Instead of:
in = keyboard.nextInt ();
while (in>=0) {
if (in>max){
in=max;
}
if (in<min){
in=min;
}
sum += in;
count++;
if (in<0){
break;
}
}
Use:
in = keyboard.nextInt ();
while (in>=0) {
if (in>max){
in=max;
}
if (in<min){
in=min;
}
sum += in;
count++;
// removed if, since loop checks it.
in = keyboard.nextInt (); // read on!
}
Upvotes: 0