0
Java - Fix errors
public class MathUtils { public static double average(int a, int b) { return a + b / 2; } public static void main(String[] args) { System.out.println(average(2,1)); } }
8 Answers
+ 6
It is returning 2.0.
+ 2
a=> int value.
2=> int value
b=>int value
a+b/2according your expression first of all opreator order is / , +
so first b/2 int / int take place.1/2=>0
now a+0 => 2+0=2
now it's return double
so 2.0 print...
for how to resolve it see
Hatsy Rei comment....
+ 1
replace line
return a + b / 2;
with
return (double)(a + b) / 2;
0
public class MathUtils {
public static double average(int a, int b) {
return (a + b) / 2.0;
}
public static void main(String[] args) {
System.out.println(average(2,1));
}
}
0
return (a + b) / 2.0;
0
return (a / 2.0 + b / 2.0);
0
public class MathUtils {
public static double average(int a, int b) {
return (a + b) / 2.0;
}
public static void main(String[] args) {
System.out.println(average(2,1));
}
}
0
public class MathUtils {
public static double average(int a, int b) {
return (a + b) / 2.0;
}
public static void main(String[] args) {
System.out.println(average(2,1));
}
}