I am writing a program to get the average of two numbers. For test case, 2, 4
the output should be 3
not 3.0
. For test case, -1282660896, -672813783
the output should be -977737339.5
not -9.777373395E8
.
My code is like this:
public static void getAve(long a, long b){
System.out.println((double)(a + b) / 2);
}
I also created a format function to meet my need, like this:
public static String fmt(double d) {
if (d == (long) d)
return String.format("%d", (long) d);
else
return String.format("%s", d);
}
But this also fails for test case 2