java 静态错误:该类没有接受 String[] 的静态 void main 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14657147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 16:59:32  来源:igfitidea点击:

Static Error: This class does not have a static void main method accepting String[]

java

提问by Falmer

I'm learning, I'm newbie but I wanted to know what I do to get "run" it. this happening a error:

我正在学习,我是新手,但我想知道我该怎么做才能“运行”它。这发生了一个错误:

Static Error: This class does not have a static void main method accepting String[].

This is the code:

这是代码:

/**
 * @author "LionH"
 */
public class Caneirinho {

    public static void contar() {
        int i = 1;
        String a = " Carneirinho",
            b = " pulando a cerca.",
            c = "s";

        for (i = 1; i <= 100; i++) {
            if (i == 1) {
                System.out.println(i + a + b);
            } else {     
                System.out.println(i + a + c + b);
            }
        }
    }
} // Carneirinho

回答by mellamokb

Any Java class that you run directly must have a mainmethod, which is the entry point, i.e., where the program starts when you execute the code.

任何直接运行的Java 类都必须有一个main方法,即入口点,即执行代码时程序开始的位置。

public static void main(String args[])

Just rename your method contar()to main(String args[])and it should work.

只需将您的方法重命名contar()main(String args[]),它应该可以工作。

回答by Smit

Alternate to @mellamokb Answer

替代@mellamokb 答案

public class Caneirinho{

 public static void contar(){
   int i = 1;
   String a = " Carneirinho",
     b = " pulando a cerca.",
     c = "s";

   for(i=1; i<=100; i++){
     if(i==1){
       System.out.println( i + a + b );
      } else {     
        System.out.println( i + a + c + b ); 
        Thread.sleep(1000);  // thread wais for 1 sec ie 1000 milisecond    
      }     
    }
  }

public static void main(String[] args){
   contar(); // call contar() from main method
}

}//Carneirinho

回答by user1658435

If you write a java program, it can have a number of classes but for all the classes to run we should have a main class which is used to implement the classes which we defined. You created a class without main in it. The program will start its execution from main.

如果您编写一个 java 程序,它可以有许多类,但是对于要运行的所有类,我们应该有一个主类,用于实现我们定义的类。您创建了一个没有 main 的类。程序将从主程序开始执行。