我目前必须制作一个播放Chuck-a-Luck游戏的程序。 Chuck a Luck是一款骰子游戏,玩家可以在三个六面骰子上下注。玩家从他们的钱包或钱包中以一定数量的欧元开始,并且可以下注任何数量的欧元,小于或等于其钱包或钱包中的当前欧元金额。下表显示了玩家可以下注的内容以及如果他们是正确的,他们将赢得什么:
玩家可以继续玩,直到他们的钱包或钱包里有钱,或者决定停止玩游戏。
Wallet theWallet = new Wallet();
double cash = 10;
JOptionPane.showMessageDialog(null,"You currently have €" + cash);
Object[] options = {"Any triple", "Big", "Field", "Small"};
int option = JOptionPane.showOptionDialog(null,"What type of bet would you like to place" , "Chuck-a-Luck", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,null,options,null);
String input = JOptionPane.showInputDialog("How much money would you like to bet? ");
Scanner scanner = new Scanner(input);
double money = scanner.nextInt();
scanner.close();
boolean enoughMoney = theWallet.get(money);
这是我在主线上的代码,这是钱包类本身。
public class Wallet { // Wallet data type
// current cash in wallet
private double cash; // invariant: cash >= 0.0
// construct wallet with zero cash
public Wallet() { cash = 0.0; }
// put an amount of money into wallet
// pre-condition: money > 0.0
public void put(double money) {
assert money > 0.0 : "Wallet put method: pre-condition violated!";
if (money > 0.0) cash = cash + money;
}
// get an amount of money from wallet
// returns true if wallet had enough cash, false otherwise
// pre-condition: money > 0.0
public boolean get(double money){
assert money > 0.0 : "Wallet get method: pre-condition violated!";
if (money > 0.0 && cash >= money) {
cash = cash - money;
return true;
}
return false;
}
// return current amount of cash in wallet
public double check() { return cash; }
// convert to a String data type value
public String toString() {
return getClass().getName() + "[cash = " + cash + "]";
}
} // end Wallet data type
我输入的任何有效投注都会返回布尔结果为false,而它应该为true。 假设我有10英镑,我下注5英镑,它返回false,它应该返回true。 有什么想法没有用? 感谢
答案 0 :(得分:4)
你必须首先在钱包里放一些钱:
Wallet theWallet = new Wallet();
double cash = 10;
theWallet.put(cash);
请记住,cash
方法中的main
变量与cash
类中的Wallet
类变量不同:
public class Wallet { // Wallet data type
// current cash in wallet
private double cash; // invariant: cash >= 0.0
// construct wallet with zero cash
public Wallet() { cash = 0.0; }
//rest of code
}
答案 1 :(得分:2)
您从未将现金添加到钱包中。
Wallet theWallet = new Wallet(); double cash = 10;
theWallet.put(cash); // MISSING!
所以,你以零现金购物:)
public Wallet() { cash = 0.0; }