2009-10-07 305 views
3

我在下面的代碼中標註了註釋的位置出現「非法表達式開始」錯誤。我該如何糾正這個錯誤?如何解決Java中的「非法表達式開始」錯誤?

class planetUfo { 
    public static void main (String[] args) { 
     // having data for counting the index 
     char letters[] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'}; 

     // initial data 
     String[] groups = {"COMETQ", "ABSTAR"}; 
     String[] comets = {"HVNGAT", "USACO"}; 


               // Problem here! 
     // to count the index 
     private void countIndex (String group, String comet) { 
             // I get here "illegal start of an expression" 



      // to have two words in the array 
      char[] name = { group, comet }; 
      // to go though the words one by one in the block of the array 
      int k = 0; 
      for (int k : name[k]) { 
       // to save each letter to an array 
       char[] words = name[k].toCharArray(); 

       int sum = 1; 
       // to loop through each character in the word 
       for (int i = 0; i < words.length; i++) { 
        // to loop through each necessary character in the alphabets 
        int j = 0; 
        for (int j = 0; j < letters.length; j++) { 
         while (letters[j] !== words[i]) { 
          // to look the index of the letter in the word 
          int indexNumber = j; 
          sum = sum * (indexNumber + 1); 
          index[k] = sum; 
          j++; 
         } 
        } 
       } 
      } 
     } 
    } 
} 

回答

9

您不能在Java中相互嵌套方法。在main()方法之外移動countIndex()

2

您錯過了主函數的關閉花括號(}) - 在countIndex函數聲明之前放置一個。您還需要從main調用countIndex,我推測(編輯:詳細說明...)