PDA

View Full Version : Problem with Finding maximum value (negative, 0, positive)

04-10-2008, 04:34 AM
This program will read n double values.
value reads the values, and valMax stores the largest value.
The values read in could be positive or 0 or negative.

import java.util.Scanner;
public class ExampleProblem {
public static void main(String[] args)
{
double value, valMax = 0.0;
int n;
Scanner scan = new Scanner(System.in);
n = scan.nextInt();
for(int j = 0; j < n; j++)
{
value = scan.nextDouble();

//valMax = value;

if(valMax < value)
{
valMax = value;
}

}
System.out.println("Largest Number is: " + valMax);
}
}

I have trouble with initialize the valMax.
When it comes to negative number, the initialized value 0.0 is always the largest... but when I set valMax = value, the result is wrong...

djr33
04-10-2008, 05:06 AM
We don't do homework problems for you. If you'd like to ask a specific question, we can try to help. After all, this is practice for your class and that's how you learn.

04-10-2008, 11:39 AM
I have tried to solve it. Not like I am asking you to solve it.
I am not asking for solution, am I? I was just confused about initialization.
But if you think inappropriate for any such question, just leave it.

kakashisan
03-11-2009, 10:44 PM
im having exactly the same problem as you man with the all negative numbers