Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 试图找到掷骰子游戏的平均掷骰次数_Java_Loops_Dice - Fatal编程技术网

Java 试图找到掷骰子游戏的平均掷骰次数

Java 试图找到掷骰子游戏的平均掷骰次数,java,loops,dice,Java,Loops,Dice,我试图模拟一个骰子游戏实验。我们的目标是找到获得相同的模具值所需的平均卷数,以显示所需的连续卷数 我的程序询问用户希望运行程序的次数。因此,它将运行循环,然后在他们得到答案后停止,然后显示抛出的次数。然后,它将重复用户指定的次数 我想从每个实验中获取totalThrows,将每个totalThrows相加,然后除以我的变量turns,得到它所需的平均投掷量 我在计算所有总抛出次数的总和时遇到了一些问题。我只能得到最后一个totalThrow。如果你们能就如何解决这个问题提出一些建议,我将不胜感激

我试图模拟一个骰子游戏实验。我们的目标是找到获得相同的模具值所需的平均卷数,以显示所需的连续卷数

我的程序询问用户希望运行程序的次数。因此,它将运行循环,然后在他们得到答案后停止,然后显示抛出的次数。然后,它将重复用户指定的次数

我想从每个实验中获取
totalThrows
,将每个
totalThrows
相加,然后除以我的变量
turns
,得到它所需的平均投掷量

我在计算所有
总抛出次数的总和时遇到了一些问题。我只能得到最后一个
totalThrow
。如果你们能就如何解决这个问题提出一些建议,我将不胜感激。我认为数组可能有帮助,但我还没有在课堂上学习数组

这是我的密码

public static void main(String[] args) {
// WRITE main's CODE HERE 
    Scanner keyboard = new Scanner(System.in);
    Random randomNumber = new Random();

    int value, turns=0, nSides, rollLength; //declare variables
    int totalThrows=0, roll=0, count=0,finish=0;

    //ask for input
    System.out.println("Please enter the number of sides (2, 4, or 6): ");
        nSides = keyboard.nextInt();

    System.out.println("Enter the value sought. Must be in the range [1," + nSides + "]: ");
        value = keyboard.nextInt();

    System.out.println("Enter the length of the run.\n" + "Remember, the bigger it is the longer it will take to find it");
        rollLength = keyboard.nextInt();

    System.out.println("Enter number of times to run the experiment:"); 
        turns = keyboard.nextInt();

    System.out.println("\n");

    do
    {   
        //Countinue loop until count = rollLength
        while(count!=rollLength){  
            roll = randomNumber.nextInt(nSides)+1;  
            totalThrows++;        //will increment after every roll

            //When roll comes up as a watched value I want to increment count by one
            if(roll==value){  
            count++;    //This should stop until count is my rollLength
            }
            else if (roll!=value){  //When an unwanted roll comes up start over
            count=0;
            }
        }
        //finish counts how many times the experiment was successful
        if (count==rollLength){
            finish++;             
        }
           System.out.println("\n"); 

           //Display totalThrows it took until rollLength variable occurs
           System.out.println("Your total rolls is: "+ totalThrows);  
    } while(finish!=turns);  //This will repeat the experiment
}

}只需在顶部声明另一个变量:

int averageThrows = 0;
每次循环结束时添加到此值:

do {
    // ...
    averageThrows += totalThrows;
} while( finish != turns );
然后除以圈数:

averageThrows /= turns;
那应该对你有用