I am trying to fix the following Java code,
I cannot figure out why the printout is still 5.
public class simpleMath
{
public static void main(String[] args)
{
int number=5;
half(number);
System.out.println("5 divided by 2 is: " + number);
}
private static double half(int number) {
number = number/2;
return number;
}
}
First you need to be aware of what types you are assigning to your variables. You should change your code to look like this:
See how I now use the returned value and how I divide by 2.0? these changes will give you the results you are looking for.