+ 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; } }
2 ответов
+ 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.)
+ 1
Thank you very much.