试图编写掷骰子游戏并遇到逻辑问题

时间:2012-12-19 10:33:21

标签: java jframe logic simulation dice

我拥有除实际游戏编码之外的所有内容。我想不出如何编写逻辑代码。我知道掷骰子的规则,但我不能(对于我的生活)想到如何编码它。我会使用switch语句,还是应该使用if语句?如果你有任何一个很好的例子(似乎通过剖析其他人的代码来学习更好)。如果您发现任何错误,请告诉我。

private class PlayButtonHandler implements ActionListener
{

    public int rollDice()
    {
        int iDie = (int)(Math.random()*6) +1;

        return (iDie);
    }

    public void writeMessage(String sOutput)
    {
        jtaGame.append(sOutput);
    }

    public void actionPerformed(ActionEvent e)
    {
        rollDice();

        int iDie1 = new rollDice();
        int iDie2 = new rollDice();

        int iSum = iDie1 + iDie2;

        String sRoll = "You rolled " + iDie1 + "+" + iDie2 + "=" + iSum + ".\n";
        String sOutput = "";
        int iPoint = 0;

        switch(iSum)
        {
            case 7:
            case 11:
                    sOutput = sRoll + "You win because you got a natural :)";
                    break;
            case 2:
            case 3:
            case 12:
                    sOutput = sRoll + "You lost because you got a crap out :(";
            default:
                    sOutput = sRoll + "Point is: " + iSum + ".";
        }


    }

}

0 个答案:

没有答案