【发布时间】:2016-07-10 19:19:22
【问题描述】:
这里对 Java 有点陌生。我想从 RouletteTester 类的 playRound() 中检测属于我的 RouletteGame 类中的 makeBet() 的打印返回语句中的“赢家”或“输家”一词。我正在尝试根据检测到的单词创建布尔语句。有没有办法做到这一点?
这是我在 RouletteGame 类中的方法:
public String makeBet(int betAmount, String betType) {
String result = this.wheel.spin();
if ((betAmount > 0) || (betAmount < credits)) {
// Determines results for color bets
if (betType == "red" || betType == "black") {
this.wheel.getColor(result);
if (this.wheel.getColor(result) == "red") {
this.credits += betAmount;
return this.wheel.getColor(result) + "- winner";
}
else if (this.wheel.getColor(result) != "red") {
this.credits -= betAmount;
return this.wheel.getColor(result) + " - loser";
}
if (this.wheel.getColor(result) == "black") {
this.credits -= betAmount;
return this.wheel.getColor(result) + " - winner";
}
else if (this.wheel.getColor(result) != "black") {
this.credits -= betAmount;
return this.wheel.getColor(result) + " - loser";
}
}
// Determines results for parity bets
if (betType == "even" || betType == "odd") {
this.wheel.getParity(result);
if (this.wheel.getParity(result) == "even") {
this.credits += betAmount;
return this.wheel.getParity(result) + "- winner";
}
else if (this.wheel.getParity(result) != "even") {
this.credits -= betAmount;
return this.wheel.getParity(result) + " - loser";
}
if (this.wheel.getParity(result) == "odd") {
this.credits -= betAmount;
return this.wheel.getParity(result) + " - winner";
}
else if (this.wheel.getParity(result) != "odd") {
this.credits -= betAmount;
return this.wheel.getParity(result) + " - loser";
}
}
// Determines results for a bet on a certain number
int intResult = Integer.parseInt(result);
int intBet = Integer.parseInt(betType);
if (intBet == intResult) {
this.credits += 35*betAmount;
return result + " - winner";
}
else {
this.credits -= betAmount;
return result + " - loser";
}
}
else if (betAmount < 0) {
this.credits = 0;
}
else if (betAmount > credits) {
betAmount = this.credits;
}
return "Please spin again";
我正在尝试从 RouletteTester 类的 playRound() 中引用我在 makeBet() 中的返回语句(如下)。在失败后(即,如果 makeBet() 中的字符串 return 语句包含“loser”),我想将赌注加倍,并且在获胜后(如果它包含“winner”这个词),我想回到原来的赌注金额。
public static int playRound(String betType, int betAmount) {
int startAmount = betAmount * RouletteTester.SPINS_PER_ROUND;
RouletteGame game = new RouletteGame();
game.addCredits(startAmount);
for (int roundNum = 0; roundNum < RouletteTester.SPINS_PER_ROUND; roundNum++) {
if (game.checkCredits() > 0) {
int nextBet = Math.min(betAmount, game.checkCredits());
game.makeBet(nextBet, betType);
}
}
return game.checkCredits() - startAmount;
【问题讨论】:
-
欢迎来到 StackOverflow。你的问题是非常不精确的。请花一分钟时间登录how to ask a question 并改进您的问题。
-
请出示您的代码。
-
请注意如何在java中比较字符串:stackoverflow.com/questions/513832/…