I have a java program that does not return the correct answer and I cannot figure out why. here's the code:
public class hello {
public static void main(String[] args) {
int a =5;
doubleNumbers(a);
System.out.println(" 5 doubled is:"+a);
}
private static void doubleNumbers(int a) {
a = 5*2;
}
}
It is my first java program after helloWorld.
Java is pass-by-value, which means that the variables passed to a function are not changed outside of it.
As this is homework I will not show you the solution, but just tell you to return the value of the calculation.
you're not returning anything from your method
change it to
private static int doubleNumbers(int a) {
return a * 2;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With