小编Jam*_*ell的帖子

用Java创建单个掷骰程序,但不是滚动300次,而是滚动320次

我一直试图在这段代码中找到错误的位置已经有一段时间了,我无法弄明白.该程序将6面模具滚动300次,然后输出每个数字滚动的时间.但出于某种原因,它不是滚动300次,而是滚动320次.我没有看到for循环有什么问题,所以我真的不知所措.

public static void dieRoll(){
    int[] roll = new int [300];
    int[] count = new int[] {1,2,3,4,5,6};

    for(int i = 1; i<300; i++){
            roll[i] = (int) Math.ceil( (int) (Math.random()*6)+1 );

//          roll[i] = (int) Math.ceil(roll[i]);
//          System.out.println(roll[i]);

            if(roll[i]==1){
                count[0]++;
            }
            else if(roll[i]==2){
                count[1]++;
            }
            else if(roll[i]==3){
                count[2]++;
            }
            else if(roll[i]==4){
                count[3]++;
            }
            else if(roll[i]==5){
                count[4]++;
            }
            else if(roll[i]==6){
                count[5]++;
            }

        //  System.out.println(roll[i]);

    }//i loop   

    System.out.println("The die landed on 1 " + count[0] + " times.");
    System.out.println("The die landed on 2 …
Run Code Online (Sandbox Code Playgroud)

java dice

1
推荐指数
1
解决办法
1727
查看次数

标签 统计

dice ×1

java ×1