【问题标题】:For Loop to score bowlingFor Loop 得分保龄球
【发布时间】:2018-05-02 22:19:54
【问题描述】:

我正在为一个潜在的实习项目工作,以获取保龄球分数的字符串输入并将它们加起来为最终分数。我很难通过我的一项测试,想知道你是否能帮我找出我的错。

不起作用的测试是 isNinetySix,它给我的结果是 98。请帮忙!

public class Game {

    private int roll = 0;
    private int[] rolls = new int[21];
    public void rolls(String scoreCard) {
        for (int i=0; i< scoreCard.length(); i++) {
            if (scoreCard.charAt(i) == 'X') {
                rolls[roll++] = 10;
            } else if (scoreCard.charAt(i) == '/') {
                rolls[roll++] = 10;
            } else if (scoreCard.charAt(i) == '-') {
            } else {
                int x = scoreCard.charAt(i);
                rolls[roll++] = x - '0';
            }
        }
    }

    public int score() {
        int score = 0;
        int cursor = 0;
        for (int frame = 0; frame < 10; frame++) {
            if (isStrike(cursor)) { 
                score += 10 + rolls[cursor+1] + rolls[cursor+2];
                cursor ++;
            } else if (isSpare(cursor)) { 
                score += 10 + rolls[cursor+2];
                cursor += 2;
            } else {
                score += rolls[cursor] + rolls[cursor+1];
                cursor += 2;
            }
        }
        return score;
    }

    private boolean isStrike(int cursor) {
        return rolls[cursor] == 10;
    }

    private boolean isSpare(int cursor) {
        return rolls[cursor] + rolls[cursor+1] == 10;
    }

    //Print scores for each frame   
    public void printFrameScore(int[] frame) {
        for (int i = 1; i < frame.length; i++) {
            System.out.println(i + ": " + frame[i]);
        }
    }

    public void displayRolls() {
        for (int i = 0; i < rolls.length; i++) {
            System.out.print(rolls[i] + ", ");
        }
    }
}

测试

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.is;

import org.junit.Before;
import org.junit.Test;

public class GameTest {

    private Game game;

    @Before
    public void setUp(){
        game = new Game();
    }

    @Test
    public void isPerfect() {
        game.rolls("X-X-X-X-X-X-X-X-X-X-XX");
        assertThat(game.score(), is(300));
    }

    @Test
    public void isGutter() {
        game.rolls("00-00-00-00-00-00-00-00-00-00");
        assertThat(game.score(), is(0));
    }

    @Test
    public void isNinety() {
        game.rolls("45-54-36-27-09-63-81-18-90-72");
        assertThat(game.score(), is(90));
    }


    @Test
    public void isOneFifty(){
        game.rolls("5/-5/-5/-5/-5/-5/-5/-5/-5/-5/-5");
        assertThat(game.score(), is(150));
    }

    @Test
    public void isNinetySix() {
        game.rolls("45-54-36-27-09-63-81-18-90-7/-5");
        assertThat(game.score(), is(96));
    }
}

【问题讨论】:

  • 是您创建的测试还是他们给您的测试?
  • 这是一个很好的例子,它的代码不起作用一个数据集来验证任何问题。赞!乔恩以后做更多这样的事情!
  • 我会试着记下这个问题,然后再回来讨论。至少值得花时间回答......

标签: java arrays string type-conversion


【解决方案1】:

这里的问题似乎是您的 isSpare() 函数永远不会返回 true,因为您为每个 / 分配了一个值 10。在一个有备用的框架中添加两个滚动的结果超过 10。如果我是你,我会尝试将/ 的分配清理为实际上是10 - prev_role_score。这比让 isSpare() 检查大于 10 更干净。还有其他方法可以清理代码,你可以尝试重构一些来打动你提交给的人。

} else if (scoreCard.charAt(i) == '/') {
    int diff = 10 - rolls[roll - 1];
    rolls[roll++] = diff;
}

【讨论】:

  • @ Jon309 现在不是最后一帧由 7 和 10 表示,最后一卷 5,你将有一个 7 和 3 帧,最后一卷 5。如果我在问你,我希望您将备用件表示为 3,因为在现实世界的模型中,3 个引脚会被击倒。这样更容易向别人解释。
  • 在现实世界中,拾取备用是用穿过框架的斜线 (/) 表示的。不管是一针、三针还是十针。
【解决方案2】:

您的代码在以下块中失败(在第 9 帧之后,您的得分为 81)。您的代码正在查看包含值 7 和您表示为 10 的 / 的索引,从而为您提供 17 而不是 10 作为备用。

        ...
        } else {
            score += rolls[cursor] + rolls[cursor+1];
            cursor += 2;
        }
        ...

所以,如果我提出建议,并且我不确定您的项目的期望是什么,我会告诉您考虑通过拆分、搜索然后添加来遍历您的字符串的更简单方法。下面是一个简单的例子:

 public void rolls(String scorecard) {
    String [] framesets = scorecard.split("-");

    //hunt for special cases like spare and strikes

    //do work to hold your scores
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-26
    • 1970-01-01
    • 2016-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-26
    相关资源
    最近更新 更多