在 Java 中声明向量时出现奇怪的错误
Weird error declaring a vector in Java
当我尝试在 Java 中声明一个向量时,我收到一条错误消息:
'Syntax error on token ";", expected "}" after this token'。我的代码中没有任何未闭合的大括号。列出了产生错误的行 below.I 用谷歌搜索了这个问题,但我看不出它有什么问题。
package gui;
import java.util.Vector;
public class PlayingCard {
private String suit;
private char rank;
private int A = 1, T = 10, J = 10, Q = 10, K = 10;
// available suits
private String[] suits = {"spades", "clubs", "diamonds", "hearts"};
Vector<String> possibleSuits = new Vector<String>(4);
for(String currentSuit:suits){
possibleSuits.add(currentSuit);
}
}
可能是因为你的 for
循环是在 class 的主体中声明的,而不是在代码块中(我认为这是不合法的) - 你可以将 for
循环放在方法中或将其放在用大括号括起来的代码块中。
将您的代码放在某种代码块中,例如方法、构造函数或静态初始化程序(在花括号 { ... }
之间)。
public class PlayingCard {
// private members...
// some method
public void someMethod() {
Vector<String> possibleSuits = new Vector<String>(4);
// This loop must be executed in a code block
for(String currentSuit:suits){
possibleSuits.add(currentSuit);
}
// Do more stuff
}
}
有关详细信息,请参阅以下资源:
当我尝试在 Java 中声明一个向量时,我收到一条错误消息: 'Syntax error on token ";", expected "}" after this token'。我的代码中没有任何未闭合的大括号。列出了产生错误的行 below.I 用谷歌搜索了这个问题,但我看不出它有什么问题。
package gui;
import java.util.Vector;
public class PlayingCard {
private String suit;
private char rank;
private int A = 1, T = 10, J = 10, Q = 10, K = 10;
// available suits
private String[] suits = {"spades", "clubs", "diamonds", "hearts"};
Vector<String> possibleSuits = new Vector<String>(4);
for(String currentSuit:suits){
possibleSuits.add(currentSuit);
}
}
可能是因为你的 for
循环是在 class 的主体中声明的,而不是在代码块中(我认为这是不合法的) - 你可以将 for
循环放在方法中或将其放在用大括号括起来的代码块中。
将您的代码放在某种代码块中,例如方法、构造函数或静态初始化程序(在花括号 { ... }
之间)。
public class PlayingCard {
// private members...
// some method
public void someMethod() {
Vector<String> possibleSuits = new Vector<String>(4);
// This loop must be executed in a code block
for(String currentSuit:suits){
possibleSuits.add(currentSuit);
}
// Do more stuff
}
}
有关详细信息,请参阅以下资源: