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 4 of 4

Thread: Whats wrong with my code? Why isn't my minimum working? Thanks.

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    3
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Whats wrong with my code? Why isn't my minimum working? Thanks.

    public class doubleNumber
    {
    public static void main (String[] args) {
    double sum = 0;
    int count = 0;
    double userEnterNumber = 0;
    double minimum = 0;
    double maximum = 0;
    double average = 0;
    System.out.println(" Input numbers, type a non-numerical digit to exit: ");
    Scanner in = new Scanner(System.in);


    while ( in.hasNextDouble())
    {
    userEnterNumber = in.nextDouble();

    if (userEnterNumber>0) {

    sum = sum + userEnterNumber;
    count ++;
    average = sum/count;
    if (userEnterNumber >maximum){
    maximum = userEnterNumber;
    }
    else if (userEnterNumber <minimum){
    minimum = userEnterNumber;
    }
    }

    }
    System.out.println(" Sum: "+sum);
    System.out.println(" Count: "+count);
    System.out.println(" Average: "+average);
    System.out.println(" Maximum: "+maximum);
    System.out.println(" Minimun: "+minimum);
    }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,718
    My Mood
    Mellow
    Thanks
    231
    Thanked 706 Times in 692 Posts

    Default Re: Whats wrong with my code? Why isn't my minimum working? Thanks.

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly using code or highlight tags per the above link.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    judyfinny (October 15th, 2014)

  4. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,973
    Thanks
    51
    Thanked 2,269 Times in 2,241 Posts

    Default Re: Whats wrong with my code? Why isn't my minimum working? Thanks.

    A trick when searching for max and min values is to initialize the variables with extreme values. Make min the largest possible value and max the smallest.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    judyfinny (October 15th, 2014)

  6. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 168 Times in 159 Posts

    Default Re: Whats wrong with my code? Why isn't my minimum working? Thanks.

    I thought all digits were numerical!

    else if (userEnterNumber <minimum){
    The way your code is written this will never be true. As per Norm's hint.

    Also, if I gave you five numbers how many times would you calculate the average? How many times does your code calculate it?
    Improving the world one idiot at a time!

  7. The Following User Says Thank You to Junky For This Useful Post:

    judyfinny (October 15th, 2014)

Similar Threads

  1. Why isn't this code working?
    By tai8 in forum What's Wrong With My Code?
    Replies: 24
    Last Post: March 19th, 2012, 07:00 PM
  2. Why isn't this code working?
    By tai8 in forum What's Wrong With My Code?
    Replies: 33
    Last Post: March 12th, 2012, 03:23 PM
  3. I don't get why this code isn't working
    By tai8 in forum What's Wrong With My Code?
    Replies: 23
    Last Post: February 20th, 2012, 11:38 AM
  4. my code isn't working an i can't figure out why
    By jack13580 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 18th, 2011, 12:21 PM
  5. Replies: 4
    Last Post: August 10th, 2011, 11:16 AM

Tags for this Thread