Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: What's wrong with my code?

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default What's wrong with my code?

    I want to pass two arrays into methods that will calculate the averages of the elements in the arrays.
    Here is my code.


    import java.util.Scanner;
    public class Averaginganarray {

    public static void main(String[]args){

    Scanner input = new Scanner(System.in);



    System.out.println("Enter in the ten numbers for the array");


    int[] x= new int[10];


    for(int i =0; i <10; i++) {




    x[i] = input.nextInt();




    }

    average(x);



    }



    public static int average(int[] array) {

    int sum = 0 ;

    for(int i =0; i<=array.length;i++) {

    sum+=array[i];
    }


    int average = sum/10;

    System.out.println("The average of the elements in the array is" + " " + average);

    return average;

    }


    public static double average(double[] array) {

    double sum = 0 ;

    for(int k=0; k<=array.length;k++) {

    sum+=array[k];
    }


    double average = sum/10;

    System.out.println("The average of the elements in the array is" + " " + average);

    return average;

    }

    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: What's wrong with my code?

    Can you explain what problems you are having?

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. What is wrong with my code?
    By docthoi in forum Java Theory & Questions
    Replies: 13
    Last Post: February 3rd, 2013, 05:33 PM
  2. What is wrong with my code?
    By connorlm3 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: January 28th, 2013, 05:44 PM
  3. What's wrong with my code
    By maronski in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 26th, 2012, 09:07 AM
  4. What is wrong in the code
    By Rajiv in forum JavaServer Pages: JSP & JSTL
    Replies: 4
    Last Post: July 29th, 2011, 12:09 PM
  5. What is wrong with my code?
    By phantom06 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 3rd, 2011, 05:21 AM