Page 3 of 3 FirstFirst 123
Results 41 to 45 of 45
Like Tree1Likes

Thread: Greater Than the Average

  1. #41
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,789
    Rep Power
    25

    Default Re: Greater Than the Average

    Do NOT hardcode numbers like 500 in a for loop. Use the array's length. If you use the length if will work for any array, not just for one that has 500 elements.

    That is the if statement for step 8).
    If you don't understand my response, don't ignore it, ask a question.

  2. #42
    jmscarlet9 is offline Member
    Join Date
    Feb 2012
    Posts
    57
    Rep Power
    0

    Default Re: Greater Than the Average

    I didn't hardcode the number, the teacher did. :)

  3. #43
    jmscarlet9 is offline Member
    Join Date
    Feb 2012
    Posts
    57
    Rep Power
    0

    Default Re: Greater Than the Average

    AHH! I got it!!!!

    Java Code:
    import java.util.Scanner;
    
    public class Test2
    {
    	public static void main(String[] args)
       {
    		// declare variables
    		int sum;
    		int average;
    
    		// populate the array list
    		int[] list = {5,10,15,20,25};
    
    		// initialize sum
    		sum = 0;
    
    		// add all values in the list to sum
    		for (int j = 0; j < list.length; j++)
    		{
    			sum = sum + list[j];
    		}
    
    		// print the sum of the values in list
    		System.out.println ("The sum of the elements is " + sum);
    
    // Add comments and code to display all elements in the 
    // list that are greater than the average value of all the elements in the list.
    		
    		average = sum / list.length;
    		
    		for (int j = 0; j < list.length; j++)
    		if (list[j] > average)
    		{
    			System.out.println(list[j]);
    		}
    	}
    }

  4. #44
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,789
    Rep Power
    25

    Default Re: Greater Than the Average

    You should always use {} with for statements and if statements. The for statement on line 31 is missing the {}s
    This is to prevent future problems when you are sloppy.

    Now on to the part where you get the input from a user.
    If you don't understand my response, don't ignore it, ask a question.

  5. #45
    jmscarlet9 is offline Member
    Join Date
    Feb 2012
    Posts
    57
    Rep Power
    0

    Default Re: Greater Than the Average

    Okay. I will add them in. THANK YOU SO MUCH!!!!!

Page 3 of 3 FirstFirst 123

Similar Threads

  1. Replies: 9
    Last Post: 06-17-2012, 05:13 AM
  2. Help: 1 is greater than 3?
    By atac57 in forum New To Java
    Replies: 3
    Last Post: 01-08-2012, 05:00 AM
  3. Need help getting average
    By soccer_kid_6 in forum New To Java
    Replies: 15
    Last Post: 09-13-2010, 12:59 AM
  4. Problem while writing to file, size greater than 15MB ..?
    By prabhurangan in forum New To Java
    Replies: 3
    Last Post: 12-30-2008, 06:28 PM
  5. Replies: 1
    Last Post: 06-30-2008, 08:14 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
  •