Results 1 to 3 of 3
  1. #1
    tonythekid92 is offline Member
    Join Date
    Sep 2013
    Posts
    1
    Rep Power
    0

    Angry Finding Min/Max/Average/Sum in an Array.

    Pretty new to Java. The error i am getting is that the min value always comes out to be 0 when I dont even enter a 0. I know this is a beginner problem but I just need some help finding the min. What am i doing wrong?
    ----------------------------

    import java.util.Scanner;

    public class Arrays {

    public static void main(String[] args)
    {
    int[]numbers=new int[5];
    int sum=0;
    int max=numbers[0];
    int min=numbers[0];
    double average=0.0;


    Scanner keyboard=new Scanner(System.in);
    System.out.println("Enter 5 numbers:");

    for(int i=0;i<5;i++)
    {
    numbers[i]=keyboard.nextInt();
    sum=sum+numbers[i];

    if (numbers[i] > max)
    {
    max=numbers[i];
    }

    else if (numbers[i] < min)
    {
    min=numbers[i];
    }

    }

    average = sum / 5;

    System.out.println("The sum of the numbers is " + sum);
    System.out.println("The max number is " + max);
    System.out.println("The min number is " + min );
    System.out.println("The average of the numbers is " + average);

    }
    }

  2. #2
    romero4742 is offline Member
    Join Date
    Mar 2012
    Posts
    27
    Rep Power
    0

    Default Re: Finding Min/Max/Average/Sum in an Array.

    Think about it you initialize an array and all the indexes are set to 0, then you set a variable called min to 0. Every time the user enters something it will be compared to min in this case always 0 therefore the min will always be 0 unless the user enters a negative number.

    Cheers

  3. #3
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default Re: Finding Min/Max/Average/Sum in an Array.

    Please use code tags when posting code samples - thanks!

Similar Threads

  1. finding maximum,minimum and average of 10 numbers
    By xxbakugan in forum New To Java
    Replies: 9
    Last Post: 11-02-2012, 10:13 AM
  2. Average of an array. Please help
    By vika in forum New To Java
    Replies: 1
    Last Post: 03-28-2011, 09:09 PM
  3. finding average in 2d arrays
    By the beginner in forum New To Java
    Replies: 18
    Last Post: 02-24-2011, 04:32 AM
  4. need some help finding average on java
    By jtw0812 in forum New To Java
    Replies: 2
    Last Post: 09-29-2010, 04:26 AM
  5. Replies: 20
    Last Post: 10-20-2008, 11:02 AM

Tags for this Thread

Posting Permissions

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