Java掷骰子游戏While循环随机数生成

标签 java if-statement random printing while-loop

好吧,我正在尝试创建一个骰子游戏,其中 args[0] 是玩游戏的次数。游戏.... 掷两个骰子,如果总和不等于 7,则将其值添加到总和中。如果总和等于 7,则游戏结束。我想跟踪所有游戏中最大的总和和最小的总和,该总和应该始终为零,因为当总和等于 7 时,它会将总和设置为 0。 这是我的代码。我不认为它打印的内容是我想要的...帮助?另外我如何在 Eclipse 中自动格式化?

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 个骰子相加时的最低值是 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;
        }

关于Java掷骰子游戏While循环随机数生成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21559777/

相关文章:

jquery - 如何知道jQuery上的具体列表?

java - 尝试从数组中获取随机元素,以便我可以将其标记为 "booked"

java - 随机发生器自动随机

java - Quartz 调度程序在作业执行前关闭

java - JPA 无法解析列/IntelliJ

java - List.get(index) 扩展另一个 List <WebElement> 的 xpath

java - 简化 if(执行某些操作)然后返回

java - 在传递给 Java 8 Streams 的映射函数中实现检查异常处理的更好方法是什么

java - if 语句只输入 if 之前打印

c++ - modulus 和 rand() 如何工作?