问题描述
这里我试图将我的'cash'变量从Player方法中包含在我的wagerBet()方法中的等式中。目前Eclipse告诉我变量'cash'不能'静态引用非静态现金'。我试着寻找关于这个问题意味着什么的解释,但我只是得到解释,使用更多的编程术语我不明白是因为我是这个东西的新手。
Here I am trying to have my 'cash' variable from the Player method be included in an equation in my wagerBet() method. Currently Eclipse is telling me that variable 'cash' cannot 'make a static reference to the non-static field cash'. I tried looking for explanations as to what this problem means but I'm just getting explanations that use even more programming terminology I don't understand being that I'm a rookie at this stuff.
class Player {
private ArrayList<Card>hand;
private double cash, bet;
public Player(double theCash)
{
cash = theCash; //'cash' variable here
hand = new ArrayList<Card>();
}
public static double wagerBet()
{
Scanner in = new Scanner(System.in);
System.out.print("Wager a bet: ");
double bet = in.nextDouble();
cash = cash - bet; // needs to be transferred here
System.out.println("You wagered " + bet + ". " + "Now you have " + cash + " cash left.");
return bet;
}
public void rewardBet(double bet)
{
cash = cash + (bet * 2); //cash and bet variable needs to be transferred here as well
System.out.println("You now have " + cash + "cash.");
}
有什么建议吗?
推荐答案
现金
变量不属于任何方法。它是该类的实例成员。您无法从静态方法访问它。如果您只需要在那里读取它的值,请将该方法设置为非静态,或将现金作为参数传递给它。
The cash
variable doesn't belong to any method. It is an instance member of the class. You can't access it from a static method. Make the method non-static, or pass 'cash' to it as a parameter if you only need to read its value there.
这篇关于如何在一个Java的另一个方法中将一个方法的变量作为变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!