Eclipse中的错误 – 找不到Mainclass

我是编程新手,我非常喜欢它。 我刚刚下载了Eclipse,我得到了一个错误,我无法帮助我。 不幸的是它用德语,但意思是这样的:“主要类没找到” – “Fehler:Hauptklasse konnte nicht gefunden oder geladen werden”

我知道它与“ public static void main(String [] args) ”有关。 由于这对我来说是全新的,所以你可以帮助我很酷。

在错误源代码下面;

 /** * Write a description of class Light here. * * @author (Sunny) * @version (31.01.2014) */ public class Elevator { // Variables int maxCarr; // max. carry in KG int currentCarr; // current state of carry measured in people boolean fillCondition; // filed or empty - value false = empty int currentStage; // stage where elevator remains // Constructor public Elevator() // initiation { maxCarr = 1600; currentCarr = 0; fillCondition = false; currentStage = 0; System.out.println("**********"); System.out.println("* *"); System.out.println("* *"); System.out.println("* *"); System.out.println("* *"); System.out.println("* *"); System.out.println("* *"); System.out.println("**********"); } public int (int carry) // Setting carry into the elevator { currentCarr = carry; if (currentCarr != 0) { fillCondition = true; return 1; } else { return 0; } } public void move(int stage) { if (stage > currentStage) { System.out.println("moving up"); currentStage = stage; } else { System.out.println("moving down"); currentStage = stage; } } } 

当你运行java时它会运行我在你的类中看不到的main方法,所以基本上eclipse告诉你:“你想让我运行什么?”,你应该实现它:

 public static void main(String[] args){ // code here } 

我发现了另一个错误。

  public int (int carry) // Setting carry into the elevator { currentCarr = carry; if (currentCarr != 0) { fillCondition = true; return 1; } else { return 0; } } 

方法不能被称为‘int’ 。 该名称由Java语言保留。

在开发core-java应用程序时,您需要做的就是在其中有一个main方法(当然具有function:P),这是JVM在您尝试运行应用程序时搜索的第一个代码片段。 对于上面的代码,试试这个:

 public static void main (String [] args) { //Now, make an instance of your class to instantiate it. Elevator obj = new Elevator(); //Then,as per the desired functionality. Call the methods in your class using the reference. //obj.move(val of stage); obj.move(10); } 

只需确保有一个执行java代码的主方法。 快乐编码:)

java的访问点是主要方法..每个程序必须从main方法访问。 在main方法中,您需要创建类的实例以使用main方法中的方法,如下所示:

 public static void main(String [] args){ Elevator elevator = new Elevator(); elevator.move(1); ... } 

还有public int (int carry) // Setting carry into the elevator在Java中public int (int carry) // Setting carry into the elevator格式无效,你必须定义一个方法名称

 public int setCarry(int carry) // Setting carry{ //your code } 

感谢您的出色答案和错误纠正。 我仍然不明白的是在哪里放置public static void main(Strng [] args)以及为什么在X位置。我的理解如下。

  1. 实例变量的定义
  2. 设置构造函数
  3. 方法的定义

我必须在1或3之后加上“public static void main(Strng [] args)”吗?

而且当我从“我的观点”看“public static void main(Strng [] args)”时,我创建了一个字符串数组,对吗? 但为什么会这样呢? 我想做的就是使用数字。

干杯谢谢你,Sunny

我刚刚编写了以下代码并且工作得非常好。

 /** * Write a description of class Light here. * * @author (Sunny) * @version (02.02.2014) */ public class Person { // Variables public int weight; // Constructors public Person() { weight = 80; } // Methods public void setPersWeight(int weight) // setting a persons weight { this.weight = weight; if (weight <= 0) { switch (weight) { case 0: System.out .println("Person must have a weight between 1-160 KG"); break; default: System.out.println("A person can't weigh less than 0KG"); break; } } else { if (weight > 160) { System.out.println("To heavy - The elevator will not work"); } else { System.out.println("The person in the Elevator weights " +weight +"KG."); } } } public static void main(String[] args) { Person Hans = new Person(); Hans.setPersWeight(-1); } } 

感谢你们所有人 – 非常酷,以获得这样的结果。

但仍然 – 你可能对我上面提到的问题有一个答案:

“而且当我从”我的角度看“public static void main(Strng [] args)时,我创建了一个字符串数组,对吗?但为什么会这样呢?我想做的就是使用数字。 “

欢呼阳光

我们不能没有运行Java程序

 public static void main(String[] args) { } 

该程序只执行主要方法。 在main方法中,您可以创建对象

 Elevator elevator = new Elevator(); 

您可以将main方法放在任何位置。