954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

What code to find MAX and MIN? [Problem]

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

fullgl
Newbie Poster
5 posts since Jul 2009
Reputation Points: 10
Solved Threads: 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.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

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[i]);
}

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.

amitrail
Newbie Poster
5 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You