gpt4 book ai didi

java - 如何更改 toString() 值

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

如何才能更改 jar1.toString() 值并将其放入player.toString() 值中。

my programs output:
"Jill[](1) [a#1](1) [b#2](2) [c#3](3) /7\(4)"
desired output when using turn() method:
"Jill[a#1](1) [](1) [b#2](2) [c#3](3) /7\(4)"

我已经做到了,当玩家输入字符串“P”进行拾取时,它会删除第一个 jar 中的值“[](1) [b#2](2) [c#3](3)",我通过将 Stone 设为 null 并在 jar 类 java if Stone == null 下执行 if 语句来做到这一点,我们返回空括号 [ ] 但尚未将其传输到player.toString() jar "Jill[](1)"

我尝试为 Class Player String toString() 方法实现 if 语句,

   public String toString()  {
return name + "[" + "]" + "(" + position + ")";
}

类似的东西返回名称+“[”+stone.toString()“]”+“(”+位置+“)”;


public class Ground
{
public Jar jar1;
private Jar jar2;
private Jar jar3;
private Player player;
private Chest chest;
private String move;

public String toString(){
return player.toString() + " " +
jar1.toString() + " " +
jar2.toString() + " " +
jar3.toString() + " " +
"/" + chest.combination + "\\" + "(" + chest.getPosition() + ")";

}

public void turn(){
System.out.print("Move (l/r/p/d): ");
move = Global.keyboard.nextLine();

if (move.equals("p") && player.getPosition() == jar1.getPosition()){
jar1 = new Jar(1, null);
}
public class Jar
{

private int position;
private Stone stone;
private Player pos;

public String toString() {
if (stone == null) {
return "[]" + "(" + getPosition() + ")";
}
return "[" + stone.toString() + "]" + "(" + getPosition() + ")";
}

public class Player
{

private String name;
private int position;
private static Jar jar;
private Stone stone;
private Ground ground;

public String toString() {
return name + "[" + "]" + "(" + position + ")";
}
public class Stone
{

private String name;
private int weight;


public Stone()
{
System.out.print("Enter stone name: ");
name = Global.keyboard.nextLine();

System.out.print("Enter stone weight: ");
weight = Global.keyboard.nextInt();
Global.keyboard.nextLine();
}

public String toString()
{
return name + "#" + weight;
}

最佳答案

更改Player.toString以包含玩家的石头。如果您还没有 stone setter,请添加一个:

public class Player {
private String name;
private int position;
private static Jar jar;
private Stone stone;
private Ground ground;

public String toString() {
return name + "[" + stone + "]" + "(" + position + ")";
}

public void setStone(Stone s) {
stone = s;
}
}

如果您还没有 stone getter,请将其添加到 Jar 中:

public class Jar
{

private int position;
private Stone stone;
private Player pos;

public String toString() {
if (stone == null) {
return "[]" + "(" + getPosition() + ")";
}
return "[" + stone.toString() + "]" + "(" + getPosition() + ")";
}

public Stone getStone() {
return stone;
}

当玩家轮流时,让他们捡起石头:

public void turn() {
System.out.print("Move (l/r/p/d): ");
move = Global.keyboard.nextLine();

if (move.equals("p") && player.getPosition() == jar1.getPosition()) {

player.setStone(jar1.getStone());
jar1 = new Jar(1, null);
}
}

关于java - 如何更改 toString() 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58398686/

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