为循环打保龄球

时间:2018-05-02 22:19:54

标签: java arrays string type-conversion

我正在开展一个潜在实习的项目,以获得保龄球分数的弦输入并将其加到最终得分。我很难通过我的一项考试,并且想知道你是否能帮助我弄清楚我的错。

无效的测试是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));
    }
}

2 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:1)

您的代码在下面的区块中失败(在第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
}