Results 1 to 4 of 4
 10262013, 12:01 PM #1Member
 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) { System.out.println("Please enter a number"); 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
Last edited by waffleb051; 10262013 at 12:23 PM.
 10262013, 01:44 PM #2
Re: Help Finding the min mean and max
min gets re written to a wrong number.
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; 10262013 at 01:53 PM.
If you don't understand my response, don't ignore it, ask a question.
 10262013, 11:08 PM #3Member
 Join Date
 Oct 2013
 Posts
 2
 Rep Power
 0
Re: Help Finding the min mean and max
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.
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.
 10262013, 11:38 PM #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?If you don't understand my response, don't ignore it, ask a question.
Similar Threads

Finding the Min and Max of numbers
By Alexis_DB in forum New To JavaReplies: 6Last Post: 09302012, 01:58 PM 
I need help finding a remainder
By MrPosotive in forum New To JavaReplies: 2Last Post: 09242012, 07:01 AM 
Finding Min value in an Array
By faust83 in forum New To JavaReplies: 3Last Post: 08242012, 04:16 PM 
Finding jar files
By DGK in forum New To JavaReplies: 3Last Post: 05092012, 09:32 PM 
finding max value
By rocky86 in forum New To JavaReplies: 6Last Post: 10102010, 02:37 AM
Bookmarks