Why doesn’t my java code execute system out. println?
I'm using the NetBeans IDE and it doesn't detect any errors I'm just curious why this code didn't execute For reference only, this is exercise 4.4 of "thinking about Java: how to think like a computer scientist"
import java.lang.Math; public class Exercise { public static void checkFermat(int a,int b,int c,int n){ if ((Math.pow(a,n))+(Math.pow(b,n))==(Math.pow(c,n)) && n!=2){ System.out.println("Holy smokes,Fermat was wrong!"); } else{ System.out.println("No,why would that work?"); } } public static void main(String args[]){ int a = 8; int b = 4; int c = 10; int n = 3; } }
Solution
You will never call the checkfermat function from main The only code executed in a java program is the code in main Any other method you define will be executed only when it is invoked from main. Therefore, your code should be:
import java.lang.Math; public class Exercise { public static void checkFermat(int a,why would that work?"); } } public static void main(String args[]){ int a = 8; int b = 4; int c = 10; int n = 3; checkFermat(a,b,c,n); //call the method here } }
In addition, your local variables a, C, and N are not automatically applied to the function You must explicitly pass them as parameters Note that the A, C, and n variables in main are completely separate from the A, C, and n variables in checkfermat: they are separate variables because they are declared in separate functions