gpt4 book ai didi

java - toString 错误返回错误结果

转载 作者:行者123 更新时间:2023-12-01 04:18:58 24 4
gpt4 key购买 nike

我的任务是创建 4 个 Java 类来启动一个纸牌游戏项目。我已经完成了其中的 3 个,但最后一个出现了 toString 错误。

这是我的游戏类的代码

public class Game implements CribbageConstants {

private Player player1, player2;
private boolean player1Deals;
private Game game;

/**
* No argument constructor - set default values for Game
*/
public Game() {
player1 = new Player("?");
player2 = new Player("?");
player1Deals = true;



}

// convenience constructor
public Game(Player player1, Player player2) {
this.player1 = player1;
this.player2 = player2;
this.player1Deals = true;

}

//mutator
public void setPlayer1(Player player1) {
this.player1 = player1;

}

public void setPlayer2(Player player2) {
this.player2 = player2;
}

public void setPlayer1Deals(boolean player1Deals) {
this.player1Deals = player1Deals;
}

public void setGame(Game game) {
this.game = game;
}

//utility methods
public Player getPlayer1() {
return player1;
}

public Player getPlayer2() {
return player2;
}

/**
* @return the player1Deals
*/
public boolean isPlayer1Deals() {
return player1Deals;
}

/**
* @return the game
*/
public Game getGame() {
return game;
}

//-----------utility methods------------

public String toString() {
return "Game between " + player1 + " and " + player2;
}

}

这是我的 testToString 代码

public void testToString() {
System.out.println("toString");
Game instance = new Game();
Player p1 = new Player("Jim");
p1.setHand(hand1);
Player p2 = new Player("George");
p2.setHand(hand2);
assertEquals("Game between ? (0) and ? (0)", instance.toString());
instance.setPlayer1(p1);
assertEquals("Game between Jim (0) and ? (0)", instance.toString());
instance.setPlayer2(p2);
assertEquals("Game between Jim (0) and George (0)", instance.toString());
}

我收到的错误是:

Failed: expected <Game between ?([0) and ? (0)]> 
but was: Game between ? ([AC, AC, AC, AC, AC, AC) @0 and ? (AC, AC, AC, AC, AC, AC) @ 0]>

注意 - AC AC AC AC AC AC 是上一类指定的默认手牌。我只是很困惑为什么玩家 1 和玩家 2 返回默认手牌而不是名称

这是我的玩家类的代码

/*
* player class
*/
package model;

public class Player implements CribbageConstants {

//-----fields------
private String name;
private Hand hand;
private int position;

//---------- Constructors --------- delete constructors for 33.3%
/**
* No argument constructor - set default values for card
*/
public Player() {
name = "?";
hand = new Hand();
position = 0;
}

// convenience constructor
public Player(String name) {
this.name = name;
hand = new Hand();
}

//------mutator-----
public void setName(String name) {
this.name = name;
}

public void setHand(Hand hand) {
this.hand = hand;
}

public void setPosition(int position) {
this.position = position;
}
//-------------- Utility methods --------------

/**
* Provide a text representation of a hand.
*
* @return the hand's cards
*/
public String getName() {
return name;
}

public Hand getHand() {
return hand;
}

public int getPosition() {
return position;
}

//-----------utility methods------------
public String toString() {
return name + " (" + hand + ")" + " @ " + position;
}
}

这是我的 Player 类的 toString 代码

public void testToString() {
System.out.println("toString");
Player instance = new Player();
assertEquals("? (AC, AC, AC, AC, AC, AC) @ 0", instance.toString());
instance.setName("Alexander");
instance.setHand(hand2);
instance.setPosition(50);
assertEquals("Alexander (7H, 8C, QS, AH, 4D, 4S) @ 50", instance.toString());
}

最佳答案

  1. 您不需要 Game 类中的 Game 类型的属性。
  2. 您需要将新创建的玩家分配给您的游戏,然后游戏才能对他们进行 toString() 操作。

关于java - toString 错误返回错误结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19172960/

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