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.

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);

}

}

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?

Re: finding maximum,minimum and average of 10 numbers

Code:

` if (n<min) {`

n=min;

}

This is the opposite way round to how you've done the 'max' part in the following lines.

Re: finding maximum,minimum and average of 10 numbers

i have tried

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.

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.

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;

Code:

`if (n < min)`

min= n;

kind regards,

Jos

Re: finding maximum,minimum and average of 10 numbers

Quote:

Originally Posted by

**xxbakugan**

Blech!

Re: finding maximum,minimum and average of 10 numbers

here's the thing.the minimum is always 0.how to set min to n?

Quote:

Today, 04:16 AMJunky

Re: finding maximum,minimum and average of 10 numbers

Originally Posted by xxbakugan

Quote:

Java Code:

1

else{}

Blech!

:(-:

Re: finding maximum,minimum and average of 10 numbers

Also (and I have to admit to only thinking of this last night)...

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).

Re: finding maximum,minimum and average of 10 numbers

Quote:

Originally Posted by

**Tolls** Also (and I have to admit to only thinking of this last night)...

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.