# Thread: Help Finding the min mean and max

1. Member Join Date
Oct 2013
Posts
2
Rep Power
0

## Help Finding the min mean and max

Ok I thought I had this code working and as it turns out I dont.
The program is supposed to get however many numbers in from the user, then end when a negative number is used.
The problem is if I run the code, as I go on the min gets re written to a wrong number.
Java Code:
```//import sextion
import java.util.Scanner;

public class ArithmeticStatistics2
{
public static void main(String[] args)
{

Scanner scan;
scan = new Scanner(System.in);

// Deceleration of variables
double usrin = 0;
double max = 0;
double mean = 0;
double min = 0;
double sum = 0;
double count = 0;
double checker = 0;
double che = 0;

//getting the variables from the user
System.out.println("This program will calculate the max min and mean of an amount of numbers.");
System.out.println("To end the list, enter a negative number");

while(count>= checker)
{
usrin = scan.nextDouble();

//entering if loop 1
if(usrin>0)
{
sum = sum + usrin;
if(usrin>=max)
{
min = max;
max = usrin;
count = count +1;
}//if loop 2
else if (usrin<=min)
{
min = usrin;
count = count +1;
}//else for loop 2

}//end if 1

else
{
che = count; //holder for the count
count = -1; // replaces the counter value to -1 to end the loop
}
}//while loop to check for negative numbers

mean = sum/che;

//math section

System.out.println("min: " + min + " max: " + max + " mean: "+ mean);

}//end method main
}//End ArithmeticStatistics```
Ive been working on this code for the last 6 hours and Im at a total loss.
Last edited by waffleb051; 10-26-2013 at 12:23 PM.  Reply With Quote

2. ## Re: Help Finding the min mean and max

min gets re written to a wrong number.
Where does the min variable get set to the wrong number? What should be the logic for setting the value of min? Does the code do that?

A trick for this kind of search is to properly initialize the min and max values to force the correct results.
One technique is to set the init value of max to a small number and the value of min to a large number.
Another technique is to set both min and max to the first number.
Last edited by Norm; 10-26-2013 at 01:53 PM.  Reply With Quote

3. Member Join Date
Oct 2013
Posts
2
Rep Power
0

## Re: Help Finding the min mean and max Originally Posted by Norm Where does the min variable get set to the wrong number? What should be the logic for setting the value of min? Does the code do that?
Starting with the number 2. It gets checked to see if it is a negative number. It isnt a negative number, it goes down into the if (usrin>=max) where it gets put into the max, the adds one to the count. It restarts the loop. It asks for another number, I enter 3. It checks again to make sure it isnt a negative. Which it is not, it goes into if (usrin>=max) at this point the min is still 2. Putting a 4 in will then change the min to 3. This happens inside of loop 2. Originally Posted by Norm A trick for this kind of search is to properly initialize the min and max values to force the correct results.
One technique is to set the init value of max to a small number and the value of min to a large number.
Another technique is to set both min and max to the first number.
What do you mean. I understand setting both the min and the max to the first number. How would i get the program to see if the next number coming in is less than the current min.  Reply With Quote

4. ## Re: Help Finding the min mean and max

see if the next number coming in is less than the current min.
Compare the next number to the current value of min
If smaller, replace current value of min with that of the next number

Where does the min variable get set to the wrong number?
There several places where the variable: min gets assigned a value. At which place does it get assigned the wrong value?  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•