Reputation: 195
I have a max function that takes 5 inputs and prints the maximum depending on what the user entered. For example:
Input: 1 2 3 2.4 0.2
Output: 3 (int)
or
Input: 1.7 2 3 4.0 3.2
Output: 4.0 (double)
I can't figure out what to do. An error occurs in the second case when 4.0 == 4. How do I bypass that?
max=(double) i;
System.out.print(" "+i);
if (Math.round(max) == max) {
System.out.print(Math.round(max));
} else {
System.out.print(max);
}
Upvotes: 0
Views: 52
Reputation: 479
In your loop running over the string representation of the numbers, just keep both the string that was largest, and its parsed double value. Then at the end just print the string corresponding to the largest value.
double max = -Double.MAX_VALUE;
String smax = null;
for (String s: args) {
double d = Double.parseDouble(s);
if (d > max) {
smax = s;
max = d;
}
}
System.out.println(smax);
Upvotes: 1