【发布时间】:2014-11-28 17:17:45
【问题描述】:
我有两个我无法解决的错误,谷歌没有让我清楚地知道问题是什么。 我得到两个编译错误,一个就行了
Random random = new Random();
在 ; 之后,说 { 预期的。下一个错误在这一行
public void newGame() {
说“令牌 newGame 上的语法错误,此令牌后应有 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]);
}
}
}
}
【问题讨论】:
-
你认为应该在什么时候执行类体中间的
for循环?你为什么这么认为? -
为什么循环
for(int i = 0; i < im.length; i++)不在方法中?