Results 1 to 5 of 5
Thread: Help me please
- 03-29-2010, 02:22 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 2
- Rep Power
- 0
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]);
}
}
}
}
- 03-29-2010, 02:32 PM #2
Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
Use code tags when posting code, that is [CODE]your code goes here[/CODE]Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 03-29-2010, 02:34 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 03-29-2010, 02:43 PM #4
Member
- Join Date
- Mar 2010
- Posts
- 2
- Rep Power
- 0
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.
- 03-29-2010, 03:34 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
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:
kind regards,Java Code:// somewhere in the body of your main( ... ) method readNumbers(numArray);
Jos


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks