Results 1 to 4 of 4
  1. #1
    waffleb051 is offline Member
    Join Date
    Oct 2013
    Posts
    2
    Rep Power
    0

    Default 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
    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 11:23 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default 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 12:53 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    waffleb051 is offline Member
    Join Date
    Oct 2013
    Posts
    2
    Rep Power
    0

    Default Re: Help Finding the min mean and max

    Quote Originally Posted by Norm View Post
    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.


    Quote Originally Posted by Norm View Post
    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.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default Re: Help Finding the min mean and max

    see if the next number coming in is less than the current min.
    Read in the next number
    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?
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Finding the Min and Max of numbers
    By Alexis_DB in forum New To Java
    Replies: 6
    Last Post: 09-30-2012, 12:58 PM
  2. I need help finding a remainder
    By MrPosotive in forum New To Java
    Replies: 2
    Last Post: 09-24-2012, 06:01 AM
  3. Finding Min value in an Array
    By faust83 in forum New To Java
    Replies: 3
    Last Post: 08-24-2012, 03:16 PM
  4. Finding jar files
    By DGK in forum New To Java
    Replies: 3
    Last Post: 05-09-2012, 08:32 PM
  5. finding max value
    By rocky86 in forum New To Java
    Replies: 6
    Last Post: 10-10-2010, 01:37 AM

Posting Permissions

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