2017-05-20 85 views
-1

沒有發現當我運行我的代碼,我收到此錯誤信息:錯誤::主要方法

Error: Main method not found in class "Class name", please define the main method as: 
    public static void main(String[] args) 
or a JavaFX application class must extend javafx.application.Application 

我的代碼:

public static void main(String[] args){ 
    public void printPhoto(int width,int height, boolean inColor){ 
     System.out.println("Width = " + width + " cm"); 
     System.out.println("Height = " + height + " cm"); 
     if(inColor){ 
      System.out.println("Print is Full color."); 
     } else { 
      System.out.println("Print is black and white."); 
     } 
     printPhoto(10,20,false); 
    } 
} 
+0

是不是錯誤不夠清楚? BTW。你需要向我們展示主要方法的代碼,如果確實存在的話。確保它是'public static void main(String args [])' –

+0

然後在你的類中創建一個'public static void main(String [] args)'? – QBrute

+0

世界的Smaile沒有以正確的語境提出問題。它是Jetbrains IDE IDE的一個問題。我也有同樣的問題。我是一個經驗豐富的程序員,我的課程和主要方法都是正確的,但是從思想社區版本運行時,它顯示出這個錯誤。 – Nitiraj

回答

2

要啓動你需要的主要Java程序這不是在你的代碼中定義的方法,你可以把它像這樣:

public class Test { 

    public void printPhoto(int width, int height, boolean inColor) { 
     System.out.println("Width = " + width + " cm"); 
     System.out.println("Height = " + height + " cm"); 
     if (inColor) { 
      System.out.println("Print is Full color."); 
     } else { 
      System.out.println("Print is black and white."); 
     } 
     // printPhoto(10, 20, false); // Avoid a Stack Overflow due the recursive call 
    } 

    //main class 
    public static void main(String[] args) { 
     Test tst = new Test();//create a new instance of your class 
     tst.printPhoto(0, 0, true);//call your method with some values   
    } 

} 
0

正如已經@YCF_L貼,你所需要的主要方法。

首先,確保你刪除:

printPhoto(10,20,false); 

,以避免由於遞歸調用堆棧溢出。

如果你需要創建一個新的實例,並直接使用你的方法,你可以做到以下幾點:

public class Test { 

    public Test printPhoto(int width, int height, boolean inColor) { 
     System.out.println("Width = " + width + " cm"); 
     System.out.println("Height = " + height + " cm"); 
     if (inColor) { 
      System.out.println("Print is Full color."); 
     } else { 
      System.out.println("Print is black and white."); 
     } 
     return this; 
    } 

    public static void main(String[] args) { 
     Test testObj = new Test().printPhoto(10, 20, false); //Creates a new instance of your class Test and calls the method printPhoto 
    } 

} 

或者,您也可以使方法靜態的,但它並不總是最好的方法,它取決於你將如何使用它。

public class Test { 

    public static void printPhoto(int width, int height, boolean inColor) { 
     System.out.println("Width = " + width + " cm"); 
     System.out.println("Height = " + height + " cm"); 
     if (inColor) { 
      System.out.println("Print is Full color."); 
     } else { 
      System.out.println("Print is black and white."); 
     } 
    } 

    public static void main(String[] args) { 
     printPhoto(10, 20, false); 
    } 

} 
相關問題