Results 1 to 5 of 5

Thread: Help me please

  1. #1
    light-flyer is offline Member
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Default Help me please

    Hey guys, im new here and need some help as i see that many of you here are good at java.

    Heres the prompt:

    Write a program that takes 10 numbers as inputs. The program then displays the average of the numbers followed by all of the numbers that are greater than the average. As part of your design, write a method that takes an array of doubles as a parameter and returns the average of the data in the array

    I have tried to do as much as i can and ill link it below but i am no good at using methods, let alone passing values to and from it. So all they help is really appreciated. Could you also let me know if im not following the prompt correctly or mis-read it.

    Thanks

    Heres what i have so far:

    import java.util.Arrays;
    import java.util.Scanner;

    public class AverageNumbers {

    public static void main (String[]args){

    Scanner reader = new Scanner (System.in);


    int size = 5;
    double sum = 0, average;
    double [] numArray = new double [size];

    for(int i = 0; i < numArray.length; i++){
    System.out.println("Please enter a number: ");
    numArray[i] = reader.nextDouble();
    }


    for(int i = 0; i < numArray.length; i++){
    sum += numArray[i];
    }

    average = sum / size;

    System.out .println("Average: " + average);

    for(int i = 0; i < numArray.length; i++){
    if(numArray[i]>average){
    System.out.println("These numbers are greater than the average: " + numArray[i]);
    }

    }

    }
    }

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    Use code tags when posting code, that is &#91;CODE]your code goes here&#91;/CODE]
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by light-flyer View Post
    Write a program that takes 10 numbers as inputs.
    Java Code:
    		int size = 5;
    		double [] numArray = new double [size];
    I guess your assignment meant 10 as in 10 decimal, not as in 10 base 5. ;-)

    kind regards,

    Jos

  4. #4
    light-flyer is offline Member
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Default

    oh sorry about that PhHein, will do thanks.

    JosAh , i did mean 10 but as im testing it im saving the trouble and time by just entering 5 integers at first.

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by light-flyer View Post
    JosAh , i did mean 10 but as im testing it im saving the trouble and time by just entering 5 integers at first.
    I was only joking, but seriously now: what actually are your problems? Creating methods? Here is one method skeleton: it is supposed to read the numbers for your array; you already have that code, you only should move it into the following method:

    Java Code:
    public static void readNumbers(double[] array) {
    
       // read numbers into the array
    
    }

    In your main( ... ) method you should call this method if you want numbers read into your array:

    Java Code:
    // somewhere in the body of your main( ... ) method
    readNumbers(numArray);
    kind regards,

    Jos

Posting Permissions

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