2013-08-23 54 views
0

我一直在編寫和調用一些方法,一個顯示當前日期和一個打印星號金字塔的方法。我已經知道代碼將被編譯,但我遇到了另一個問題。當我嘗試在JGrasp中運行時,出現錯誤:以下文件缺少類或內部類文件:(文件名)。不建議在糾正之前進行。。在互聯網上沒有發現任何關於此事的信息,我再次嘗試,但在Eclipse中。我這次得到的錯誤信息是:選擇不包含主要類型選擇不包含主類型

我真的很感激,如果有人可以概述我的代碼,並告訴我他們是否看到了問題。一個解釋也會很棒,因爲我正在努力學習和更好地成爲一名程序員。謝謝!!

import java.util.Calendar; 

public class prcMeth { 

    public static void showCurrentDate() { 
     Calendar cal = Calendar.getInstance(); 
     int month = cal.get(Calendar.MONTH); 
     int day = cal.get(Calendar.DATE); 
     int year = cal.get(Calendar.YEAR); 
     System.out.println(year + "-" + month + "-" + day + " "); 
    } 

    public static boolean printPyramid(int n) { 
     int number = n; 
     if (number <= 0 || number > 10) { 
      System.out.println("Your parameter is invalid; you input n = " + n); 
      return false; 
     } 
     for (int row = 1; row <= number; row++) { 
      for (int space = number; space > row; space--) { 
       System.out.print(" "); 
      } 
      for (int star = 1; star <= row; star++) { 
       System.out.print("*"); 
       System.out.println(); 
      } 
     } 
     return true; 
    } 

    public static void main(String[] args) { 

     System.out.println("Hello, world!"); 

     // Calling first function 
     showCurrentDate(); 

     // Calling second function 
     System.out.println("n=0"); 
     printPyramid(0); 

     System.out.println("n=1"); 
     printPyramid(1); 

     System.out.println("n=5"); 
     printPyramid(5); 

     System.out.println("n=10"); 
     printPyramid(10); 

     System.out.println("n=11"); 
     printPyramid(11); 
    } 
} 
+1

該消息意味着Eclipse無法找到主要方法。可能你正在運行項目而不指定主類。用主方法編輯文件時按F11可以避免它。在Eclipse中,我可以使用 –

+1

。儘管類名通常以大寫字母開頭。此外,這些方法不需要是靜態的。如果它們不是靜態的,你應該實例化你的類的一個對象。 –

+0

[錯誤:選擇不包含主要類型]的可能重複(http://stackoverflow.com/questions/16225177/error-selection-does-not-contain-a-main-type) – jww

回答

0

我認爲有這個程序沒有任何問題,因爲當我在記事本程序複製並嘗試運行這個比它成功運行。所以沒有這個代碼的問題。

唯一的一個邏輯問題是

for (int star = 1; star <= row; star++) { 

      System.out.print("*"); 

      System.out.println(); 

     } 

應該

for (int row = 1; row <= number; row ++){ 

     for (int space = number; space > row; space--){ 

      System.out.print(" "); 

     } 

     for (int star = 1; star <= row; star++) { 

      System.out.print("*"); 

     } 
     System.out.println(); 

    } 

以適當的格式打印金字塔。當你的System.out.println()在內部循環中時,當前代碼全部以新行打印。

0

我測試過你的代碼工作正常。在eclipse中如果你有任何問題需要運行代碼,那麼它可能是eclipse不能精細化的主要方法。因此,做一件事 右鍵單擊代碼並選擇運行爲運行配置,在那裏您可以提供主類和與JVM相關的所有其他條目

0

您在Eclipse中描述的問題很容易發生,比實際的課程重點。例如,即使您在編輯器中打開了您的類,並且它是唯一打開的類,但是如果您有(例如)控制檯窗格爲重點,Eclipse將在控制檯中查找主,並且當然找不到一。

相關問題