我如何解决Java中“非法表达式”的错误?(How do I fix an “Illegal start of expression” error in Java?)

编程入门 行业动态 更新时间:2024-10-28 09:19:01
我如何解决Java中“非法表达式”的错误?(How do I fix an “Illegal start of expression” error in 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++; } } } } } } }

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

最满意答案

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

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

更多推荐

本文发布于:2023-08-07 04:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1461153.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表达式   如何解决   错误   Java   fix

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!