【问题标题】:Is there any way to parse through a method's return statement in order to detect a certain word?有没有办法解析方法的返回语句以检测某个单词?
【发布时间】: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;

【问题讨论】:

标签: java parsing return


【解决方案1】:

使用 string.contains("winner") 。我认为这就是你所需要的。它返回一个布尔值。

【讨论】:

    【解决方案2】:

    正如第一位评论员所说,这个问题非常不精确。请让您的问题更精确一些和/或提供一些代码 sn-p。

    无论如何,如果你想检查一个字符串是否包含一些字符序列,你可以使用 .contains() 方法。

    否则你可以使用 .equals() 方法来检查你的字符串是否等于别的东西。

    基于此,您可以在您的类/方法中检查您想要的字符串的其他方法的返回值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-25
      • 1970-01-01
      • 2013-07-02
      • 1970-01-01
      • 2014-04-05
      • 2019-12-23
      相关资源
      最近更新 更多