gpt4 book ai didi

java - 不确定这个类是如何工作的

转载 作者:行者123 更新时间:2023-12-01 07:57:23 25 4
gpt4 key购买 nike

我目前必须制作一个玩 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 的 boolean 结果,而它应该是 true。假设我有 10 英镑,我下注 5 英镑,它在应该返回 true 的地方返回 false。关于什么不起作用的任何想法?谢谢

最佳答案

您必须先在钱包里存一些钱:

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
}

关于java - 不确定这个类是如何工作的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28284464/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com