Results 1 to 10 of 10
  1. #1
    xxbakugan is offline Member
    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Default finding maximum,minimum and average of 10 numbers

    hello,i am a complete beginner to java programming.what's wrong with this program is how do i find the minimum of the given numbers.what value do i need to assign to the min variable?any help would be appriciated.

    Java Code:
    import java.util.Scanner;
    public class onem {
      public static void main (String[]args) {
      Scanner k=new Scanner(System.in);
      
      int sum=0;
      int i=1;
      int min=0;
      int max=0;
      int avg=0;
      
      while(i<=10) {
        int n;
        System.out.println("write a number");
        n=k.nextInt();
        
        if (n<min) {
        n=min;
        
        
        }
        else{}
        if (n>max) {
        max=n;
        }
        sum=sum+n;
        i++;
      }
      avg=sum/10;
      System.out.println(min);
      System.out.println(avg);
      System.out.println(max);
      
      
      }
    }

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

    Default Re: finding maximum,minimum and average of 10 numbers

    There are a ton of ways to handle this case. I bet if you think about it a little harder you'll get it. What have you tried?
    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
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default Re: finding maximum,minimum and average of 10 numbers

    Java Code:
        if (n<min) {
        n=min;
        }
    This is the opposite way round to how you've done the 'max' part in the following lines.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: finding maximum,minimum and average of 10 numbers

    i have tried

    Java Code:
    if (n<min) //here's the problem{
    n=min;
    }
    but the problem is variable min is set to 0.so the output is always 0.but the goal of the program is to find the minimum of the given numbers.so how do i set the variable min to the variable n.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default Re: finding maximum,minimum and average of 10 numbers

    No.
    You want to change the 'min' value if 'n' is smaller.
    At the moment you are changing the 'n' value.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,734
    Blog Entries
    7
    Rep Power
    21

    Default Re: finding maximum,minimum and average of 10 numbers

    Assignments work 'from right to left', i.e. a= b means that a gets the value of b; so in your example it should be;

    Java Code:
    if (n < min)
       min= n;
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: finding maximum,minimum and average of 10 numbers

    Quote Originally Posted by xxbakugan View Post
    Java Code:
    else{}
    Blech!

  8. #8
    xxbakugan is offline Member
    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Default Re: finding maximum,minimum and average of 10 numbers

    Java Code:
    int min=0;
    here's the thing.the minimum is always 0.how to set min to n?

    Today, 04:16 AMJunky
    Re: finding maximum,minimum and average of 10 numbers

    Originally Posted by xxbakugan
    Java Code:
    1
    else{}
    Blech!

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default Re: finding maximum,minimum and average of 10 numbers

    Also (and I have to admit to only thinking of this last night)...
    Java Code:
    int min = 0;
    Unless you can guarantee that there is going to be at least one 'n' less than 0 then the 'min' value will always be 0 as n will never be less than 0.
    You should assign a really big number to 'min', and a really big negative number to 'max' (the Integer class has a couple of constants).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    xxbakugan is offline Member
    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Default Re: finding maximum,minimum and average of 10 numbers

    Quote Originally Posted by Tolls View Post
    Also (and I have to admit to only thinking of this last night)...
    Java Code:
    int min = 0;
    Unless you can guarantee that there is going to be at least one 'n' less than 0 then the 'min' value will always be 0 as n will never be less than 0.
    You should assign a really big number to 'min', and a really big negative number to 'max' (the Integer class has a couple of constants).
    thanks a lot.

Similar Threads

  1. Replies: 2
    Last Post: 07-27-2011, 08:35 PM
  2. Replies: 8
    Last Post: 03-26-2011, 12:38 AM
  3. Maximum Minimum Values
    By z6StringAssasin in forum New To Java
    Replies: 2
    Last Post: 05-09-2010, 01:36 AM
  4. Calculating Maximum and Minimum
    By Ejava in forum New To Java
    Replies: 6
    Last Post: 03-01-2010, 05:36 PM
  5. To find the Maximum and Minimum in an Array of Strings
    By luscious in forum JavaServer Pages (JSP) and JSTL
    Replies: 9
    Last Post: 07-31-2008, 02:51 PM

Posting Permissions

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