How do I get the variable of one method to be a variable in another method for Java?

前端 未结 4 771
南笙
南笙 2021-01-28 22:44

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\' c

相关标签:
4条回答
  • 2021-01-28 23:21

    You might want to go through some Java basics:

    http://docs.oracle.com/javase/tutorial/java/javaOO/index.html

    You are attempting to use a property of Player in a static method. A static method is not part of an instance so it doesn't know what "cash" (as it's unique to every Player instance rather than a single variable).

    Remove the "static" from wagerBet so that it becomes a method of Player. That way it's unique to every Player and so it'll know to use the "cash" of the same Player it's a part of.

    0 讨论(0)
  • 2021-01-28 23:21

    remove static at

    public static double wagerBet()
    
    0 讨论(0)
  • 2021-01-28 23:23

    There is only one real problem. To understand what static is.

    Basic approach, until you are good enough, is to NOT use static at all. Remove it in all cases instead of "main" (you have to keep it there).

    0 讨论(0)
  • 2021-01-28 23:27

    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.

    0 讨论(0)
提交回复
热议问题