2015-08-26 31 views
-2

我想在NetBeans中運行一些Java代碼,它一直告訴我沒有主類。我已經看過其他類似的問題,但他們不幫助解決我的特定問題。如何解決「<找不到主要課程>」?

即使我添加主循環,它仍然告訴我沒有主類。此代碼的目的是創建一個名爲VolcanoRobot的類,該類可以在另一個程序中使用,或者我可以添加public static void main(String args[])並運行此代碼。

嘗試這兩種方法編譯器仍然有這個相同的問題。這裏是我的代碼有使用類:

class VolcanoApplication { 
    public static void main(String[] args){ 
    VolcanoRobot dante = new VolcanoRobot(); 
    dante.status = "exploring"; 
    dante.speed = 2; 
    dante.temperature = 510; 

    dante.showAttributes(); 
    System.out.println("Increasing speed to 3."); 
    dante.speed = 3; 
    dante.showAttributes(); 
    System.out.println("Changing temperature to 670."); 
    dante.temperature = 670; 
    dante.showAttributes(); 
    System.out.println("Checking the temparature"); 
    dante.checkTemperature(); 
    dante.showAttributes(); 


}} 

VolcanoRobot的創作:

class VolcanoRobot { 
     String status; 
     int speed; 
     float temperature; 

     void checkTemperature() { 
      if (temperature > 600) { 
       status = "returning home"; 
       speed = 5; 
      } 
     } 

     void showAttributes() { 
      System.out.println("Status: " + status); 
      System.out.println("Speed: " + speed); 
      System.out.println("Temperature: " + temperature); 
     } 
    } 
+2

你有其他類或這都是你的代碼? - 事實上你需要有一個包含主要方法的公共類。如果你沒有,那麼這就是問題所在。 – Leah

+0

你如何運行你的項目? – JeredM

+0

我正在使用NetBeans運行它。 –

回答

0

你需要調用您VolcanoRobot這樣一個主類:

public class MyMain 
{ 
    public static void main(String[] args) 
    { 
     VolcanoRobot bot = new VolcanoRobot(); 
     bot.showAttributes(); 
     bot.checkTemperature(); 
     bot.showAttributes(); 
    } 
} 
0

此代碼不包含static void main(String[] args)。主類是java用作程序入口點的類。或者更具體地說,public static void main(String[] args)是入口點。或分別調用啓動程序的方法。由於此代碼不包含main方法,因此java不知道從何處開始程序並抱怨該問題。

這在Oracle Java的教程描述here

相關問題