2012-12-08 61 views
0

出於某種原因,我得到一個語法錯誤,說「語法錯誤,插入」}「以完成ClassBody。」我檢查了每個方法,每個語句以確保我有完整的開放和關閉括號,所以我不知道爲什麼會發生這種情況。有人可以告訴我爲什麼我可能會遇到這個問題嗎?語法錯誤,請插入「}」以完成ClassBody?

將代碼複製到另一個文件中並不能解決問題,也不會執行Project> Clean。

import java.util.Scanner; 

public class jloneman_Numerology 
{ 
    private String[] report; 
    private int day, month, year, num; 

    public jloneman_Numerology() 
    { 
     introduction(); 
     report = new String[9]; 
     num = 0; 
    } 

    public void introduction() 
    { 
     System.out.println("Welcome to ACME Numerology Reports! We will " + 
       "determine your special\nnumerology report based on your " + 
       "birth date.\n"); 
    } 

    public void getDate() 
    { 
     char slash1, slash2; 

     do 
     { 
      System.out.print("Please enter your birth date (mm/dd/yyyy): "); 
      Scanner in = new Scanner(System.in); 
      String date = in.nextLine(); 

      month = in.nextInt(); 
      day = in.nextInt(); 
      year = in.nextInt(); 

      slash1 = date.charAt(3); 
      slash2 = date.charAt(8); 
     } while (validDate(slash1, slash2) == false); 

     calcNum(); 
    } 

    public boolean validDate(char slash1, char slash2) 
    { 
     boolean isValid = true; 

     // Check for valid month 
     if (month < 1 || month > 12) 
     { 
      isValid = false; 
      System.out.printf("Invalid month: %d\n", month); 
     } 

     // Check for valid day 
     if (day < 1 || day > 31) 
     { 
      isValid = false; 
      System.out.printf("Invalid day: %d\n", day); 
     } 

     // Check for months with 30 days, else 31 days = invalid 
     if ((month == 4 || month == 6 || month == 9 || month == 11) && (day < 1 || day > 30)) 
     { 
      isValid = false; 
      System.out.printf("Invalid day: %d\n", day); 
     } 
     else if (day < 1 || day > 31) 
     { 
      isValid = false; 
      System.out.printf("Invalid day: %d\n", day); 
     } 

     // Check for valid year 
     if (year < 1880 || year > 2280) 
     { 
      isValid = false; 
      System.out.println("Please enter a valid year between 1880 and 2280."); 
     } 

     // Check for correct separating character 
     if (slash1 != '/' || slash2 != '/') 
     { 
      isValid = false; 
      System.out.println("Invalid separating character, please use forward slashes"); 
     } 

     if (leapYear() == true) 
     { 
      if (month == 2 && day > 29) 
      { 
       isValid = false; 
       System.out.printf("Invalid day for 2/%d: %d", year, day); 
      } 
     } 

     return isValid; 
    } 

    public boolean leapYear() 
    { 
     boolean isLeap; 

     if (year % 4 == 0 && year % 400 != 0) 
      isLeap = false; 
     else 
      isLeap = true; 

     return isLeap; 
    } 

    public void calcNum() 
    { 
     // Separate each digit of the date and add to a single number 

     // Test number for debugging 
     num = 5; 
    } 

    public void printReport() 
    { 
     report[0] = ":1: "; 
     report[1] = ":2: "; 
     report[2] = ":3: "; 
     report[3] = ":4: "; 
     report[4] = ":5: "; 
     report[5] = ":6: "; 
     report[6] = ":7: "; 
     report[7] = ":8: "; 
     report[8] = ":9: "; 

     System.out.println(report[num]); 
    } 
} 

                                        78,0-1  Bot 
+0

我複製你的代碼到我的工作區,我看到沒有任何形式的編譯器錯誤:P – PermGenError

+0

它看起來不錯 - 你顯示所有的該文件的內容?名爲'jloneman_Numerology.java'的文件是? – assylias

+0

你的代碼最後是什麼'78,0-1 Bot'?它是罪魁禍首嗎?我懷疑是這樣。無論如何,請告訴我們。 :) – Sufian

回答

3

嘗試刪除(或註釋掉)一種方法,看看問題是否存在。如果有,請刪除或註釋掉其他方法等等,直到錯誤消失。然後恢復一切,但最後的方法。

如果錯誤不再出現,問題可能出現在最後一個方法中。

如果它確實重新出現,問題更加微妙;也許是嵌入代碼中的控制字符。嘗試將代碼複製並粘貼到純文本編輯器中(因此任何控制字符都將被忽略,保存並重新編譯)。

0

我也有這個錯誤 上面的代碼沒有錯誤(當我使用上面作爲一個子類的代碼)。 我被清理我的IDE的工作空間,並確保呼叫者工作正常糾正我的錯誤。

  • 項目設置
  • 運行設置
  • 確認在主被正確列出
  • 公共靜態無效的主要(字串[] args){...}
相關問題