Basically, I am making a gambling game. It asks the person how much they want to bet, then if they are right, their 'wallet' increases by however much they win back, depending on the odds given. I am using a class exclusively for the persons 'wallet'.

public class Wallet {

    private double cash;

    public Wallet() {cash=0.0;}

    public void put(double money) {
        assert money > 0 : "Die : pre-condition violated ! ";

        cash=cash+money;
    }

    public boolean get(double money) {
        if (money>0 && cash>=money) {
            cash=cash-money;
            return true;
        }
        return false;
    }

    public double count() {return cash;}

    public String toString(){
        return getClass().getName()+ "[cash = ]" + cash + "]";
    }

I'm having trouble calling the put() method outside of this class. How should my line of code be written for it to be called? I am using the variable name money to store the double, should I be doing that, or a different variable name altogether.

有帮助吗?

解决方案 2

Wallet walletObject = new Wallet();
walletObject.put(money);

其他提示

Wallet wallet = new Wallet();
wallet.put(money);

As the other answers say its:

double money = 123.123;
Wallet wallet = new Wallet();
wallet.put(money);

The variable name money within the class has absolutely no impact on how to call the method from the outside. You can also call the method by

double val = 123.123;
Wallet wallet = new Wallet();
wallet.put(val);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top