java “令牌 ********* 上的语法错误,此令牌后应为 annotationName”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27193390/
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
"Syntax error on token *********, annotationName expected after this token"
提问by Rewbert
i am having two errors i can not solve, google didn't give me a clear idea of what the problem was. I get two compiling errors, one on the line
我有两个我无法解决的错误,谷歌没有让我清楚地知道问题是什么。我收到两个编译错误,一个就行
Random random = new Random();
right after the ;, saying { expected. The next error is at this line
紧跟在 ; 之后,说 { 预期。下一个错误是在这一行
public void newGame() {
saying "Syntax error on token newGame, annotationName expected after this token". What does this mean? I have an extra } at the bottom of my code, the compiler (Eclipse) complains if i remove this. It says } expected on the last } if i remove it.
说“令牌 newGame 上的语法错误,此令牌后需要 annotationName”。这是什么意思?我的代码底部有一个额外的 },如果我删除它,编译器 (Eclipse) 会抱怨。如果我删除它,它会说} 预期在最后一个}。
Any pointers in the right direction is welcome, but no spoonfeeding please. :) I want to learn. If i am breaking java convention anywhere, please point that out aswell. Thanks!
欢迎任何正确方向的指示,但请不要用勺子喂食。:) 我想学习。如果我在任何地方违反了 Java 约定,也请指出这一点。谢谢!
whole code:
整个代码:
import java.awt.*;
import java.io.*;
import javax.swing.*;
import java.util.Random;
public class Memory {
File folder = (new File("mypictures"));
File[] pictures = folder.listFiles();
ImageIcon im = new ImageIcon();
Card[] allCards;
Random random = new Random();
for(int i = 0; i < im.length; i++) {
allCards[i] = new Card(new ImageIcon(pictures[i].getPath()));
}
public void newGame() {
int row = Integer.parseInt
(JOptionPane.showInputDialog("How many rows?"));
int column = Integer.parseInt
(JOptionPane.showInputDialog("How many columns?"));
Card[] game = new Card[row*column];
for(i = 0; i < game.length; i++) {
int ranint = random.nextInt(game.length);
game[i] = allCards[ranint];
Card c = game[i].copy();
game[i+game.length/2] = c;
}
for(i = 0; i < 5; i++) { // Randomizing a few times.
Tools.randomOrder(game);
}
JFrame jf = new JFrame("Memory");
jf.setLayout (new GridLayout (row, column));
for(i = 0; i < game.length; i++) { // Adds the cards to our grid.
jf.add(game[1]);
}
}
}
}
回答by Gabriel Oliveira
Your first loop needs to be put inside a method of the class. In case you want that loop to be executed upon the creation of an object of such class, you must write a constructor method like this:
您的第一个循环需要放在类的方法中。如果您希望在创建此类对象时执行该循环,则必须编写如下构造函数方法:
public Memory() {
for(int i = 0; i < im.length; i++) {
allCards[i] = new Card(new ImageIcon(pictures[i].getPath()));
}
}
However, you can't assign values to an array this way, because allCards
is just an empty variable holding null
. You must initialize the variable like this:
但是,您不能以这种方式为数组赋值,因为allCards
它只是一个包含null
. 您必须像这样初始化变量:
Card [] allCards = new allCards[desiredLength];
回答by Mureinik
The problem is the first for loop. In Java, you cannot just put code under a class - it needs to be in a method, constructor or anonymous block. Since this seems like intinialization code, a constructor seems appropriate:
问题是第一个 for 循环。在 Java 中,您不能只将代码放在类下——它需要放在方法、构造函数或匿名块中。由于这看起来像是初始化代码,因此构造函数似乎很合适:
public class Memory {
File folder = (new File("mypictures"));
File[] pictures = folder.listFiles();
ImageIcon im = new ImageIcon();
Card[] allCards;
Random random = new Random();
/** Defaylt constructor to initialize allCards: */
public Memory() {
allCards = new Crad[im.length];
for(int i = 0; i < im.length; i++) {
allCards[i] = new Card(new ImageIcon(pictures[i].getPath()));
}
}
// rest of the class