为循环打保龄球

Jon*_*309 6 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] + ", ");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

测试

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));
    }
}
Run Code Online (Sandbox Code Playgroud)

Ben*_*enD 3

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

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