gpt4 book ai didi

java - 如何正确引用此代码中的 gem 重量?

转载 作者:行者123 更新时间:2023-12-01 19:34:22 26 4
gpt4 key购买 nike

如何在这段代码中引用石头的重量,以便我可以在unlockChest方法中使用它?我基本上试图将用户从对象 new Stone()) 输入的权重相加,因此它 == 组合了用户在 Chest() 构造函数中输入的值。我知道这是错误的,但我尝试了 jar1.weight,它说它不是变量。


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 class Jar
{

private int position;
private Stone stone;


public Jar()
{
position = 0;
stone = null;
}

public Jar(int initPos, Stone stone)
{
position = initPos;
this.stone = stone;
}

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

public Ground()
{
player = new Player();

jar1 = new Jar(1, new Stone());
jar2 = new Jar(2, new Stone());
jar3 = new Jar(3, new Stone());

chest = new Chest()

}

public boolean ChestUnlocked()
{
if (jar1.weight + jar2.weight + jar3.weight == chest.combination) //the main problem
return true;
else
return false;
}


public class Chest
{

public int combination;
private int position;
private Jar jar1;
private Jar jar2;
private Jar jar3;

public Chest()
{
System.out.print("Enter chest combination (5-10): ");
combination = Global.keyboard.nextInt();
Global.keyboard.nextLine();


position = 4;
jar1 = new Jar(4, null);
jar2 = new Jar(4, null);
jar3 = new Jar(4, null);
}

最佳答案

Stone,或许,Stone 类中的权重属性是私有(private)的。为了正确使用它们,我建议您为您的类实现 getter 方法。实现 getter 后,您可以使用如下方式调用它们:

jar1.getStone().getWeigth()

其中 getStone() 是 Jar 类中的 getter,getWeight() 是 Stone 类中的 getter。

关于java - 如何正确引用此代码中的 gem 重量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58377375/

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