Java骰子滚动游戏while循环Randomon生成

时间:2014-02-04 17:52:38

标签: java if-statement random printing while-loop

好吧所以我试图制作一个骰子游戏,其中args [0]是玩游戏的次数。游戏......滚动两个骰子,如果总和不等于7,则将它们的值加到总和中。如果总和等于7则游戏结束。我想跟踪所有游戏中最大的总和以及最小值应该为零,因为当总和等于7时,它将总和设置为0。 这是我的代码。我不认为它是什么印刷是我想要的...帮助?另外我如何在日食中自动格式化?

public class diceGame {
    public static void main(String[] args) {
        int dice1;
        int dice2;
        int count=0;
        int theSum=0;
        int lowest=500;
        int finalSum=0;
        int diceSum=0;
        while (count !=Integer.parseInt(args[0])){
            count=count+1;
            theSum=0;
            while(diceSum!=7){
                diceSum=0;
                dice1=1 + (int)(Math.random() * ((6 - 1) + 1));
                dice2=1 + (int )(Math.random() * ((6 - 1) + 1));
                diceSum=dice1+dice2;
                if (diceSum !=7){
                    theSum=theSum+diceSum;
                if (theSum>finalSum){
                    finalSum=theSum;
                    }
                if (theSum<lowest){
                    lowest=theSum;
                }


                }

                }
            }
        System.out.println("After "+args[0]+" simulations: ");
        System.out.println("Biggest sum: "+finalSum);
        System.out.println("Smallest sum: "+lowest);
    }
    }

我修好了

public class diceGame {
    public static void main(String[] args) {
        int dice1;
        int dice2;
        int count = 0;
        int theSum = 0;
        int lowest = Integer.MAX_VALUE;
        int finalSum = 0;
        int diceSum;
        int totalSum=0;
        while (count < Integer.parseInt(args[0])) {
            count = count + 1;
            diceSum=0;
            theSum=0;
            while (diceSum!=7) {
                diceSum = 0;
                dice1 = 1 + (int) ((Math.random() * (6 - 1)) + 1);
                dice2 = 1 + (int) ((Math.random() * (6 - 1)) + 1);
                diceSum = dice1 + dice2;
                if (diceSum != 7) {
                    theSum = theSum + diceSum;
                }
                //System.out.println("the sum is "+theSum);
            }
            if (theSum > finalSum) {
                finalSum = theSum;
            }
            if (theSum < lowest) {
                lowest = theSum;
            }
            totalSum=totalSum+theSum;
        }
        double average=(double)totalSum/(Double.parseDouble(args[0]));
        System.out.println("After " + args[0] + " simulations: ");
        System.out.println("Biggest sum: " + finalSum);
        System.out.println("Smallest sum: " + lowest);
        System.out.println("The average is: "+average);

    }
}

2 个答案:

答案 0 :(得分:1)

这是因为添加时2个骰子的最低值是2,而不是0.如果你在第一个卷上滚动7,那么你将不会更新你的最大值和最低值。您需要将这些检查移到循环之外。

        while(diceSum!=7){
            diceSum=0;
            dice1=1 + (int)(Math.random() * ((6 - 1) + 1));
            dice2=1 + (int )(Math.random() * ((6 - 1) + 1));
            diceSum=dice1+dice2;
            if (diceSum !=7) {
                theSum=theSum+diceSum;
            }
        }
        if (theSum>finalSum){
            finalSum=theSum;
        }
        if (theSum<lowest){
            lowest=theSum;
        }

答案 1 :(得分:1)

如果diceSum为7,则不会执行最小/最大检查,因为您将其置于if(diceSum!= 7)的大括号中。因此,如果diceSum为7,则不会更新mininum。 最低总和也不总是0.例如:

First diceroll:
Dice1: Value 5
Dice2: Value 3

这使得diceSum = 8。

So it's not equal 7, so theSum becomes 0+8=8
Because 8>0 (theSum>finalSum) finalSum gets updated: finalSum=8;
Because 8<500 (theSum<lowest) lowest gets updated: lowest=8;

Next dice roll
Dice1: Value 4
Dice2: Value 3
diceSum=7

即使你纠正你的大括号

 while (count !=Integer.parseInt(args[0])){
    count=count+1;
    theSum=0;
    while(diceSum!=7){
        diceSum=0;
            //Corrected Random Brace (see comment below your question)
        dice1=1 + (int)((Math.random() * (6 - 1)) + 1);
        dice2=1 + (int)((Math.random() * (6 - 1)) + 1);
        diceSum=dice1+dice2;
        if (diceSum !=7){
                theSum=theSum+diceSum;
        } // CHANGED BRACE POSITION HERE
        if (theSum>finalSum){
            finalSum=theSum;
        }
        if (theSum<lowest){
            lowest=theSum;
        }
    }
}

最低值仍将设为8.不为零。如果在第一骰子掷骰中骰子总数为7的情况下存在游戏回合,则最低值仅为零。