如何修复 Java 中的“非法开始的表达式”错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1534263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 13:54:58  来源:igfitidea点击:

How do I fix an "Illegal start of expression" error in Java?

java

提问by Léo Léopold Hertz ??

I am getting an "Illegal start of expression" error in the following code at the location marked by a comment. How can I correct this error?

我在以下代码中由注释标记的位置收到“非法开始的表达式”错误。我该如何纠正这个错误?

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

采纳答案by Greg Hewgill

You can't nest methods within one another in Java. Move countIndex()outside the main()method.

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

回答by flatline

You are missing a closing curly-brace (}) for your main function - place one before the countIndex function declaration. You will also need to call countIndex from main, I presume (edit: elaborated...)

您的主函数缺少右花括号 (}) - 在 countIndex 函数声明之前放置一个。您还需要从 main 调用 countIndex,我想(编辑:详细说明...)