+ 1

Java: Why is there an initialization error when my variable is initialized?!?!

public class Exponent { public static void main(String[] args) { int number = 3; int result; int upTwo = square(number, result); int upThree = cube(number, result); System.out.println("3^2 = " + upTwo); System.out.println("3^3 =" + upThree); } public static int square(int num, int squareMainVars) { squareMainVars = num * num; return squareMainVars; } public static int cube(int three, int cubeMainVars) { cubeMainVars = three * three * three; return cubeMainVars; } }

14th Aug 2018, 8:33 PM
Fleet
Fleet - avatar
2 odpowiedzi
+ 5
You are passing result by value and never put a value in it. Making changes to squareMainVars does not touch result. Personally, I'd delete result, remove the second parameter from both square & cube, add int before your now undefined locals, and rename them to something that makes more sense (updating both usages.)
14th Aug 2018, 8:57 PM
John Wells
John Wells - avatar
+ 1
Thank you very much.
15th Aug 2018, 11:43 AM
Fleet
Fleet - avatar