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)); } }

8th Jun 2018, 5:31 AM
Siddiqa Ahmed
Siddiqa Ahmed - avatar
8 ответов
+ 6
It is returning 2.0.
8th Jun 2018, 5:34 AM
☇☇The Flash☇☇
☇☇The Flash☇☇ - avatar
+ 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....
8th Jun 2018, 8:21 AM
Nitish kumar jha
Nitish kumar jha - avatar
+ 1
replace line return a + b / 2; with return (double)(a + b) / 2;
2nd Jan 2021, 7:36 AM
prajapati ankit
prajapati ankit - avatar
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)); } }
8th Jun 2018, 5:37 AM
Hatsy Rei
Hatsy Rei - avatar
0
return (a + b) / 2.0;
15th Jun 2019, 7:03 AM
Mohammed Irfan Kasim
Mohammed Irfan Kasim - avatar
0
return (a / 2.0 + b / 2.0);
26th Jan 2020, 11:39 AM
Nidhin .M.P
Nidhin .M.P - avatar
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)); } }
10th Dec 2020, 10:28 AM
Manimaran O
Manimaran O - avatar
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)); } }
11th Mar 2022, 3:13 AM
Ricardo Mancha