Java保龄球计划 - 添加第10帧

时间:2016-04-13 22:27:56

标签: java class loops methods junit

我目前正在开展一项保龄球计划并且大部分都在工作。我有一个名为BowlingGameTest的junit测试文件,一个名为Game的类,用于处理大部分评分,还有一个名为Play的类,用于处理大部分游戏本身。据我所知,唯一需要添加的是第10帧细节。现在,如果我罢工"然后第10帧就像帧1-9那样结束。然而,它需要给我另一个碗,并根据我"碗"下一次投掷,也可能是另一个碗。实现第10帧的最佳方法是什么?

以下是我目前在Play课程中的内容:

import java.util.Random;

public class Play {


    public static void main(String[] args) {
        Game g = new Game();
        int i;
        int PinsDown1;
        int PinsDown2;
        int PinsDown3;

        for (i=1; i<11; i++)
        {
            PinsDown1 = ThrowBall(0); //1st try
            System.out.println("Frame " + i + " Throw 1" + " knocked down is " + PinsDown1);
            if(PinsDown1 == 10)
            {
                g.roll(10);
                System.out.println("Congratulations, you bowled a strike!");
                System.out.println();
            }
            else
            {
                PinsDown2 = ThrowBall(PinsDown1); //2nd try
                System.out.println("Frame " + i + " Throw 2" + " knocked down is " + PinsDown2);
                g.roll(PinsDown1);
                g.roll(PinsDown2);
                if (PinsDown1+PinsDown2 == 10)
                    System.out.println("Congratulations, you bowled a spare!");
                    System.out.println();
            }
        }

        System.out.println("Final score is " + g.score() );

    }  //*** end main ***

    public static int ThrowBall(int PinsDown)
    {
        int standing = 10-PinsDown;
        Random rand = new Random();
        int  down = rand.nextInt(standing+1); 
        return down;
    }

}  //*** End class ***

1 个答案:

答案 0 :(得分:2)

考虑将for循环更改为while循环。使用你的变量i作为帧计数器,如果&gt; = 10而不是攻击,则添加条件以摆脱循环。