使用此功能,我试图查看myPlayerUp(播放器对象)是否等于播放器矢量myPlayers中的任何播放器对象。它说“方法myPlayers(int)未定义类型”,即使上面使用了向量。我要以错误的方式比较这个吗?这是全班
package model;
//import yahtzee.model.boolean;
import java.util.Vector;
//import yahtzee.model.int;
//import yahtzee.model.void;
import model.Player;
public class YahtzeeEngine {
public static final int MAX_ROLLS= 0;
private Vector<Player> myPlayers;
private Roller myRoller;
private Player myPlayerUp;
private int myNumberRollsUsed;
public Vector myPlayer;
public YahtzeeEngine(int numPlayers) {
myPlayers = new Vector<Player>(numPlayers);
myNumberRollsUsed = 0;
}
public void startGame() {
int i;
for(i = 0; i<=myPlayers.size(); i++){
myPlayers.add(new Player());
}
this.selectPlayerUp(myPlayers.get(0));
myNumberRollsUsed = 0;
myRoller = new Roller();
myPlayerUp = new Player();
}
public void selectPlayerUp(Player player) {
myPlayerUp = player;
}
public void switchPlayerUp() {
int x;
int y;
for(x = 0; x<=myPlayers.size(); x++){
if(myPlayerUp == myPlayers(x) && x < myPlayers.size()-1){
y = x;
}
if(myPlayerUp == myPlayers(x) && x == myPlayers.size()-1){
y = 0;
}
}
myPlayerUp = myPlayers(y);
}
public boolean incrementRollsUsed() {
myNumberRollsUsed++;
return false;
}
public void resetNumberRollsUsed() {
myNumberRollsUsed = 0;
}
public Roller getRoller() {
return myRoller;
}
public Player getPlayerUp() {
return myPlayerUp;
}
public Vector<Player> getPlayers() {
return myPlayers;
}
public int getNumberRollsUsed() {
return myNumberRollsUsed;
}
public String toString() {
return null;
}
}
这就是它给我“myPlayers”载体上的错误
public void switchPlayerUp() {
int x;
int y;
for(x = 0; x<=myPlayers.size(); x++){
if(myPlayerUp == myPlayers(x) && x < myPlayers.size()-1){
y = x;
}
if(myPlayerUp == myPlayers(x) && x == myPlayers.size()-1){
y = 0;
}
}
myPlayerUp = myPlayers(y);
}
答案 0 :(得分:0)
向量不是函数。你想要get()
。
另外,请改用ArrayList
。
答案 1 :(得分:0)
您需要indexOf
类的Vector
方法。像
y = myPlayers.indexOf(myPlayerUp);
答案 2 :(得分:0)
您正在尝试访问像myPlayers(x)
这样的向量中的元素,但是这就是调用方法myPlayers
,它接受一个int,并且没有这样的方法。
您想要做的是:
myPlayers.get(x);
同样为了进行比较,如果你想比较两个玩家在他们的领域是相同的,而不是他们的参考,你可能想要使用equals
。
您需要覆盖equals
答案 3 :(得分:0)
要添加SLaks所说的,在Java中,我们通过覆盖equals()
方法来测试对象之间的相等性。
==
不会提供您想要的结果,只能用于比较基元的相等性。
答案 4 :(得分:0)