Results 1 to 6 of 6
  1. #1
    Wizard0860 is offline Member
    Join Date
    Nov 2012
    Posts
    29
    Rep Power
    0

    Default Math.min not working.

    User inputs 10 #'s, program tells user sum, average, max, and min. I'll ad average later, but why isnt the min working
    Java Code:
       import java.util.Scanner;
       public class Driver01
       {
          public static final int NUMITEMS=10;
          public static void main (String[] args)
          {
             double[] array=new double[NUMITEMS];
             Scanner keyboard=new Scanner(System.in);
             double sum=0;
             double average=0;
             double min=array[0];
             double max=array[0];
             for(int x=0;x<NUMITEMS;x++)
             {
                System.out.print("#"+(x+1)+": ");
                array[x] = keyboard.nextDouble();
             }
             for(int x=0;x<NUMITEMS;x++)
             {
                sum = array[x]+sum;
             }
             average =sum/10.0;
             
             for(int x=0;x<NUMITEMS;x++)
             {
                min = Math.min(min, array[x]);
             }
             
             for(int x=0;x<NUMITEMS;x++)
             {
                max=Math.max(array[x],max);
             }
             System.out.println("Sum: " +sum);
             System.out.println("Minimum: " +min);
             System.out.println("Maximum: " +max);
          }
       }
    Last edited by Wizard0860; 02-27-2013 at 08:19 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,037
    Rep Power
    10

    Default Re: Math.min not working.

    What do you mean when you say it doesn't work?

    What happened when you stepped through this with a debugger, or at least added some print statements, to figure out what's going on?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,017
    Rep Power
    6

    Default Re: Math.min not working.

    Ok, here are a few observations.

    Don't assign min and max until "after" you populate the array. Otherwise, you assign both a default double value which is 0. That is why your program is not working correctly.

    Also, a few changes could make it shorter.


    1. Assign sum to be equal to array[0] (after the array is filled of course).
    2. Then you can use one loop for everything, starting at x = 1.
    3. Then calculate average outside the loop
    4. You already have a variable named NUMITEMS. So when taking the average, divide by that instead of an explicit 10.


    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    Wizard0860 is offline Member
    Join Date
    Nov 2012
    Posts
    29
    Rep Power
    0

    Default Re: Math.min not working.

    Thanks Jim! It is fixed now.

  5. #5
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,017
    Rep Power
    6

    Default Re: Math.min not working.

    I also noticed that you are calculating average but not printing it out.

    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    Wizard0860 is offline Member
    Join Date
    Nov 2012
    Posts
    29
    Rep Power
    0

    Default Re: Math.min not working.

    Yeah, but as I said in the 1st line of the thread, I'll add it later.

Similar Threads

  1. Math.sqrt not working!
    By atiyarose17 in forum New To Java
    Replies: 15
    Last Post: 10-10-2012, 01:55 AM
  2. Math.sqrt is not working
    By GoingDarkness in forum NetBeans
    Replies: 5
    Last Post: 04-08-2012, 06:31 AM
  3. Replies: 2
    Last Post: 01-04-2011, 05:30 AM
  4. Create Math.sin without math.sin
    By vudoo in forum New To Java
    Replies: 11
    Last Post: 12-07-2010, 07:23 AM
  5. Replies: 8
    Last Post: 05-28-2008, 08:00 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
  •