Compilered, but not run - Exception in thread "main" java.lang.NoSuchM

/* A Method called Factorial
* Using perimater in Main function
* to pass some variable to Factorial
* Return a Factorial Vaule to main

public class FactorialMath{
private static final int MAX_Num=4; // Calculate 4! = 4*3*2*1

public void main(String[] args){
for (int i=0; ijavac

D:java-code>java FactorialMath
Exception in thread "main" java.lang.NoSuchMethodError: main


  • Hey,
    Code has 3 issues. The "main" method in a class must be declared static. ie.[code]public [color=Orange]static[/color] void main(String[] args) {[/code]
    From a static context either a class must be instantiated and its members referenced or static class members used, therefore to conform with the latter approach the following method declaration change is required: [code]private [color=Orange]static[/color] int Factorial(int n) { // added "static" modifier[/code]
    Lastly, a typo will cause an infinite loop, [code]// for (int i = 1; 1 <= n; i++) { // loop control intended to use i not 1
    for (int i = 1; [color=Orange]i[/color] <= n; i++) {[/code]

    regards, se52
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


In this Discussion