0

What code to find Max value and min value in this Array... ? Help me Please..?

import java.util.Scanner;
import java.lang.Math;
class Work4_1
{
    public static void main(String[] args)
    {
        Scanner input=new Scanner(System.in);
        int[] value=new int[10];
        int max,min;
        for(int i=1;i<value.length;i++)
        {
            System.out.print("Enter Value : ");
            value[i]=input.nextInt();
        }
        Math.max(value[i]); <<< It's True...?
        Math.min(value[i]);  <<< It's True...?
        }
}

Thanks

Edited by Reverend Jim: Fixed formatting

3
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by amitrail
0

As you get each number you need to compare it with the current max and min values. If it's less than the current min, it becomes the new min, ditto for max.
You can use Math.max if you like, it needs TWO parameters and returns the larger of the two, but I recommend that you do not use it, just code the if test yourself. That way youi'll learn more, and will be in better shape for tracking which element of the array is largest, if you need to.

0

Use the following code, after you take the value in your array

TreeSet ts = new TreeSet();

for(int i = 0; i < value.length; i++)
{
ts.add(value);
}

the numbers will be automatically sorted. Pick the first and last number as the max and min numbers required.

Check out
SNIP
for more help on implementing this type of code.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.