Java &引用;令牌*******上出现语法错误,此令牌后面应为annotationName;

Java &引用;令牌*******上出现语法错误,此令牌后面应为annotationName;,java,this,token,Java,This,Token,我有两个错误我不能解决,谷歌没有给我一个问题是什么清楚的想法。 我有两个编译错误,一个在线 Random random = new Random(); 紧接着;,说{预期。下一个错误在这行 public void newGame() { 说“token newGame上的语法错误,此token后应为annotationName”。这是什么意思?我的代码底部有一个额外的},如果我删除它,编译器(Eclipse)会抱怨。如果我移除它,它会显示}在最后一个}预期 欢迎任何指向正确方向的指针,但请不

我有两个错误我不能解决,谷歌没有给我一个问题是什么清楚的想法。 我有两个编译错误,一个在线

Random random = new Random();
紧接着;,说{预期。下一个错误在这行

public void newGame() {
说“token newGame上的语法错误,此token后应为annotationName”。这是什么意思?我的代码底部有一个额外的},如果我删除它,编译器(Eclipse)会抱怨。如果我移除它,它会显示}在最后一个}预期

欢迎任何指向正确方向的指针,但请不要使用勺子。)我想学习。如果我在任何地方违反了java惯例,请也指出这一点。谢谢

全部代码:

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]);
        }
    }
}
}
import java.awt.*;
导入java.io.*;
导入javax.swing.*;
导入java.util.Random;
公共类内存{
文件夹=(新文件(“mypictures”);
File[]pictures=folder.listFiles();
ImageIcon im=新的ImageIcon();
卡片[]所有卡片;
随机=新随机();
for(int i=0;i
问题是循环的第一个问题。在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
公共类内存{
文件夹=(新文件(“mypictures”);
File[]pictures=folder.listFiles();
ImageIcon im=新的ImageIcon();
卡片[]所有卡片;
随机=新随机();
/**定义用于初始化所有卡的构造函数:*/
公共内存(){
allCards=新Crad[im.长度];
for(int i=0;i
需要将第一个循环放入类的方法中。如果希望在创建此类对象时执行该循环,则必须编写如下构造函数方法:

public Memory() {
    for(int i = 0; i < im.length; i++) {
        allCards[i] = new Card(new ImageIcon(pictures[i].getPath()));
    }
}
Card [] allCards = new allCards[desiredLength];

你认为什么时候< <代码> >循环,在类体的中间,应该执行什么?为什么这样认为?为什么循环<代码>为(int i=0;i<im.长度;i++)不在方法中?此外,
所有卡都需要初始化first@JeffShaw很好的解决方法。回答得很好,既然你这么说了,它就完全可以解释为什么它不起作用了。谢谢!