我的问题是如何将值添加到另一个类的变量中,例如:
private int balance;
然后稍后在同一类的方法中,我使用以下方法将值添加到余额中没有问题:
balance += 50;
但是当我去另一个班级时,输入对象然后是余额的设置器:
myObject.setBalance(50);
但是问题出现在这里,当我去头等舱并从那里归还余额时,我什么也没得到(或余额的旧值),换句话说,余额的新值没有被添加。任何想法为什么?在过去的几个小时里,我一直被困在这一点上
这是我的代码:
public class Zoo
{
private int balance;
public void setBalance(int balance)
{
this.balance = balance;
}
public int getBalance()
{
return this.balance;
}
}
MY SECOND CLASS:
public class RandomEvents
{
private Zoo ZooBalance = new Zoo();
public void callEventSix()
{
System.out.println("A local group has raised money to your Zoo");
System.out.println("Would you like to accept the money? (y) or (n)");
Scanner readerEventThree = new Scanner(System.in);
String readerTwo = readerEventThree.next();
if ( readerTwo.equals("y") )
{
ZooBalance.setBalance(166);
System.out.println("You have accepted the gift");
System.out.println("Your new balance is " + ZooBalance.getBalance());
} else if ( readerTwo.equals("n") )
{
System.out.println("You have refused the gift");
}
}
}