我提示用户输入他们的名字以及在我的ArrayList中添加ai播放器。不幸的是,我收到了一个"找不到符号"单词错误"添加"在我的代码下面。我假设它没有导入某些东西,我现在似乎无法弄明白。
package connectfour;
import java.util.ArrayList;
import javax.swing.*;
import userInterface.Connect4Ui;
import core.Player;
import core.HumanPlayer;
import core.AiPlayer;
/**
*
* @author j_ortiz9688
*/
// connect 4 main
public class ConnectFour {
private static ArrayList<Player>player;
private static Connect4Ui frame;
public static void makePlayers(){
player = new ArrayList<Player>();
String name = JOptionPane.showInputDialog("Enter your name");
HumanPlayer player = new HumanPlayer(name);
AiPlayer ai = new AiPlayer("Computer", 0);
player.add(player);
player.add(ai);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
frame = new Connect4Ui();
}
}
答案 0 :(得分:5)
您正在创建一个与ArrayList
同名的新变量,它隐藏了类中的静态ArrayList
字段(即使您删除对{{1的调用)也不会编译}})。您需要重命名本地.add()
变量或player
字段。
例如:
player
答案 1 :(得分:2)
您定义一个本地变量HumanPlayer player = new HumanPlayer(name);
隐藏您的同名ArrayList
数据库。只要给它一个不同的名字,你应该没问题:
HumanPlayer human = new HumanPlayer(name); // Here!
AiPlayer ai = new AiPlayer("Computer", 0);
player.add(human); // and use it here, of course
player.add(ai);