java - 如何修复 Java 中的 "Illegal start of expression"错误?

标签 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++;
                        }
                    }
                }
            }
        }
    }
}

最佳答案

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

关于java - 如何修复 Java 中的 "Illegal start of expression"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1534263/

相关文章:

java - 将 API 与其实现分离

java - 如何在recyclerview适配器中使用getItematposition()作为ListView

java - 使用 ViewModel 时如何获取插入实体的 ID?

java - Tomcat 无法启动

java - 当 Eden 未满时发生 Minor GC。当Eden未满时,会出现Minor GC的原因是什么?

java - Web 应用程序连接到外部服务器?

java - 有没有办法使用 Apache 的 HttpClient 获取网站的证书?

java - 来自 URL 的 GWT RPC 调用

java - Dockerfile 中的打包步骤是下载依赖项,尽管前面有一个步骤来解决依赖项

java - android.os.NetworkOnMainThreadException 里面新线程