【发布时间】:2018-03-01 21:14:23
【问题描述】:
所以我有一个在主程序中创建的数据类whiplash,在这个类里面是一个玩家的实例变量,它包含x数量的玩家和他们的变量
public class Whiplash {
private int rounds;
private WhiplashQuestions questions;
private Player[] players;
public Whiplash(int ROUNDS, WhiplashQuestions QUESTIONS, Player[] PLAYERS) {
setRounds(ROUNDS);
setPlayers(PLAYERS);
setWhiplashQuestions(QUESTIONS);
}
public int getRounds() {
return rounds;
}
public void setRounds(int rounds) {
this.rounds = rounds;
}
public Player[] getPlayers(int id) {
return players[id];
}
public void setPlayers(Player[] players) {
this.players = players;
}
public WhiplashQuestions getQuestions() {
return questions;
}
public void setWhiplashQuestions(WhiplashQuestions question) {
this.questions = question;
}
}
public class Player {
private int id;
private int points;
private String player;
public Player(int ID, int POINTS, String PLAYER) {
setId(ID);
setPoints(POINTS);
setPlayer(PLAYER);
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getPoints() {
return points;
}
public void setPoints(int points) {
this.points = points;
}
public String getPlayer() {
return player;
}
public void setPlayer(String player) {
this.player = player;
}
}
else if (command.startsWith("#whiplash")) {
int usercount = getUserCount(client);
String users[] = getUsersInRoom(client);
Player[] player = new Player[usercount];
player = convertUsersToPlayers(users, usercount);
WhiplashQuestions q = new WhiplashQuestions("Whats your favorite color? ");
Whiplash wl = new Whiplash(10, q, player);
startWhiplash(wl);
public Player[] convertUsersToPlayers(String[] users, int userCount) {
Player p[] = new Player[userCount];
int u = users.length;
for (int i = 0; i < users.length; i++) {
p[i] = new Player(i, 0, users[i]);
}
return p;
}
public int getUserCount(ConnectionToClient client) {
String room = client.getInfo("room").toString();
int count = 0;
Thread[] clientThreadList = getClientConnections();
for (int i = 0; i < clientThreadList.length; i++) {
ConnectionToClient clientProxy = ((ConnectionToClient) clientThreadList[i]);
if (clientProxy.getInfo("room").equals(room)) {
count++;
}
}
return count;
}
public String[] getUsersInRoom(ConnectionToClient client) {
String room = client.getInfo("room").toString();
int connectedUsers = getUserCount(client);
String[] usersInRoom = new String[connectedUsers];
Thread[] clientThreadList = getClientConnections();
for (int i = 0; i < clientThreadList.length; i++) {
ConnectionToClient clientProxy = ((ConnectionToClient) clientThreadList[i]);
if (clientProxy.getInfo("room").equals(room)) {
try {
//System.out.println(clientProxy.getInfo("userName".toString()));
usersInRoom[i] = clientProxy.getInfo("userName".toString()).toString();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
return usersInRoom;
}
因此,在我的主程序中,我通过将播放器字符串转换为播放器类型,然后传递正确的值来创建鞭打实例。我希望能够从主程序访问各个 Player[] 索引,并且对这样做的语法感到困惑。例如,在主程序中,如果我想打印一个whiplash实例的第三个玩家玩家字符串,我可以按照 System.out.print(whiplash.getPlayers[3].getPlayers().toStrng( )); ?
【问题讨论】:
-
你的主程序在哪里?
-
和 [3] 实际上是第四个元素。在 Java 中,数组是基于 0...
-
添加主程序,并注意索引。
-
另外我没有看到你在哪里覆盖了
toString()方法,所以它可能会输出Object.toString()没有告诉你什么玩家...... -
你有什么问题?