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