gpt4 book ai didi

将 int 更改为整数时 Java 测试失败

转载 作者:行者123 更新时间:2023-11-30 02:53:55 25 4
gpt4 key购买 nike

我有方法electCouncillor从数组 CouncillorsSet 中获取某个议员,将其放在某个balcony上队列,从中获取额外的议员 balcony并将其放回 CouncillorSet ,为了确定将从数组中取出哪位议员,我有私有(private) int 字段 CouncillorNumber ,然后我意识到我不能使用 int 因为每次我使用该方法时我都必须重置 CouncillorNumber value 和 int 不允许为 null(如果我输入 0,它将始终使用数组中的第一个议员),所以我决定将其更改为整数,但由于某种原因测试失败了,我不明白为什么。

public class Player {
private int id;
private int councillorNumber;

public void electCouncillor(Region region,GameBoard gameBoard){
//Gets a certain councillor from the array CouncillorSet
Councillor councillor = gameBoard.getCouncillorsSet().get(councillorNumber);
//Adds that councillor to the queue Balcony
region.getBalcony().add(councillor);
//Sets the councillor on top of the Balcony in the CouncillorSet
gameBoard.setCouncillor(region.getBalcony().element());
//Removes the councillor on top of the balcony
region.getBalcony().remove();
//Removes the councillor from CouncillorSet that was added to the balcony
gameBoard.getCouncillorsSet().remove(councillorNumber);


}
public int getCouncillorNumber() {
return councillorNumber;
}
public void setCouncillorNumber(int councillorNumber) {
this.councillorNumber = councillorNumber;
}

}

这是测试

@Test
public void testElectCouncillor(){
Player player = new Player(1);
GameBoard gameBoard = new GameBoard();
//First i create 6 councillors to be used in the test
Councillor councillor1 = new Councillor();
Councillor councillor2 = new Councillor();
Councillor councillor3 = new Councillor();
Councillor councillor4 = new Councillor();
Councillor councillor5 = new Councillor();
Councillor councillor6 = new Councillor();
councillor1.setColor(Color.BLACK);
councillor2.setColor(Color.BLUE);
councillor3.setColor(Color.ORANGE);
councillor4.setColor(Color.PURPLE);
councillor5.setColor(Color.WHITE);
councillor6.setColor(Color.PINK);
//Then i add the first 4 councillors to a balcony
Region region = gameBoard.getRegionCoast();
Queue<Councillor> balcony = region.getBalcony();
balcony.add(councillor1);
balcony.add(councillor2);
balcony.add(councillor3);
balcony.add(councillor4);
//The i add the remaining two to the CouncillorSet of the GameBoard
gameBoard.setCouncillorSet(new ArrayList<Councillor>());
gameBoard.getCouncillorsSet().add(councillor5);
gameBoard.getCouncillorsSet().add(councillor6);
//This gets the first element from CouncillorSet, which in this case is the councillor5 color white
player.setCouncillorNumber(0);
player.electCouncillor(region,gameBoard);
//After doing the method first i verify that the element in top of the queue is
//now the councillor Blue
assertEquals(Color.BLUE,region.getBalcony().element().getColor());
//Then i verify that the elements 0 and 1 from the CouncillorSet are the
//Pink and Black councillors respectively (before the method the pink
//councillor was the element 1)
assertEquals(Color.BLACK,gameBoard.getCouncillorsSet().get(1).getColor());
assertEquals(Color.PINK,gameBoard.getCouncillorsSet().get(0).getColor());
}

这是使用整数而不是 int 的方法的版本

public class Player {
private int id;
private Integer councillorNumber;

public void electCouncillor(Region region,GameBoard gameBoard){
Councillor councillor = gameBoard.getCouncillorsSet().get(councillorNumber);
region.getBalcony().add(councillor);
gameBoard.setCouncillor(region.getBalcony().element());
region.getBalcony().remove();
gameBoard.getCouncillorsSet().remove(councillorNumber);


}
public Integer getCouncillorNumber() {
return councillorNumber;
}
public void setCouncillorNumber(Integer councillorNumber) {
this.councillorNumber = councillorNumber;
}

}

运行相同测试时出现的错误是 expected <BLACK>, but was <PINK>

最佳答案

取自API,根据参数类型有不同的方法。第一个删除指定索引中的元素,而第二个删除元素本身,无论索引如何

remove(int index)Removes the element at the specified position in

remove(Object o) Removes the first occurrence ofthe specified element from this list, if it is present.

编辑添加示例:

列表 = {1, 4, 5, 6, 9}

With int => remove(4)(删除索引为4的数字)结果:列表 = {1, 4, 5, 6}

With Integer => remove(4)(删除值为 4 的元素)结果:列表 = {1, 5, 6, 9}

关于将 int 更改为整数时 Java 测试失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37807224/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com