Craps游戏:Java编程

时间:2016-11-12 20:52:18

标签: java

这是我的代码我想要刺激掷骰子游戏:我收到错误

线程“main”中的异常java.lang.ArithmeticException:/ by zero     在Dice.main(Dice.java:71)

请帮助说明: 在掷骰子游戏中,传球线下注如下进行。两个六面骰子滚动;掷骰子中的第一骰子被称为“滚出来”。一个7或11的自动获胜,并且自动输出2,3或12的滚动。如果在出来的卷筒上滚动4,5,6,8,9或10,则该数字成为该点。玩家继续滚动骰子直到7或点滚动。如果该点首先滚动,则玩家赢得下注。如果首先掷出7,则玩家输掉。使用这些规则编写一个模拟掷骰子游戏的程序,无需人工输入。该计划不应要求下注,而应计算玩家是赢还是输。

程序应模拟滚动两个骰子并计算总和。添加一个循环,以便程序播放10,000个游戏。添加计数器,计算玩家获胜的次数,以及玩家输了多少次。在10,000场比赛结束时,计算获胜的概率[即Wins /(Wins + Loss)]并输出该值。从长远来看,谁将赢得大多数比赛,你还是房子?注意:要生成随机数x,其中0x≤< 1,使用x = Math.random(); 。例如,乘以6并转换为整数会得到一个介于0和5之间的整数。

public class Dice 
{
    public static void main(String[]args)
    {
        //declaring variables
        int comeOutRoll1, comeOutRoll2;
        int roll1, roll2;
        int numW, numL;
        int sum, sum2 = 0;
        int thePoint = 0;
        double probability; 


        //initializing variables
        comeOutRoll1 = (int)(Math.random()*6) +1;
        comeOutRoll2 = (int)(Math.random()*6) +1;
        sum = comeOutRoll1 + comeOutRoll2;
        numW = 0;
        numL = 0;


        for(int timesPlayed = 0; timesPlayed <= 10000; timesPlayed++)
        {

            switch(sum)
            {
            //adds how many wins and losses
            case 2:
            case 3:
            case 12:
                numL = numL + 1;
            break;
            case 7:
            case 11:
                numW = numW +1;
            break;
            case 4:
                thePoint = sum;
            case 5:
                thePoint = sum;
            case 6:
                thePoint = sum;
            case 8:
                thePoint = sum;
            case 9:
                thePoint = sum;
            case 10:
                thePoint = sum;
            break;
            //if not any of these cases roll again
            default:

                roll1 = (int)(Math.random()*6) +1;
                roll2 = (int)(Math.random()*6) +1;
                sum2 = roll1 + roll2;
                break;
            }

            if(sum2 == thePoint)
            {
                do
                numW = numW +1;
            }
            else if(sum2 == 7)
            {
                numL = numL +1;
            }
        }


            probability = (numW/(numW+numL));

            System.out.println("Number of Wins: " + numW);
            System.out.println("Number of Losses: " + numL);
            System.out.println("The probability of winning is: " + probability + " percent");   


    }

}

0 个答案:

没有答案