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.
I have trouble with initialize the valMax.Code: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); } }
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...



Reply With Quote

Bookmarks