Java 掷两个骰子并返回计数

Java 掷两个骰子并返回计数,java,dice,Java,Dice,我正在编写一个java程序来模拟两个骰子的滚动。对于每一个骰子,如果两个骰子没有相同的面,我增加一个计数器。直到两个骰子有相同的面,我想打印出计数器值。计数基本上是计算在两个骰子上获得相同面孔的次数 我试着写: int count = 0; while (true) { int dice1 = getRandom(1,6); int dice2 = getRandom(1,6); if (dice1 != dice2) { count ++;

我正在编写一个java程序来模拟两个骰子的滚动。对于每一个骰子,如果两个骰子没有相同的面,我增加一个计数器。直到两个骰子有相同的面,我想打印出计数器值。计数基本上是计算在两个骰子上获得相同面孔的次数

我试着写:

int count = 0;

while (true) {
    int dice1 = getRandom(1,6);
    int dice2 = getRandom(1,6);

    if (dice1 != dice2) {
        count ++;
    }
System.out.println(count);
代码似乎没有产生正确的输出。我很好奇根据我的逻辑我是否正确地模拟了它?我是java新手,希望能在这方面得到一些帮助

函数的作用是:返回一个介于1和6之间的随机数,因为骰子可以有1到6个值

private static int getRandom(int n1, int n2){
    int retVal = 0;

    retVal = n1 + (int) Math.floor(Math.random() * (n2 - n1 + 1));

    return retVal;
}

你总是在打印计数。只有当数字相等时,才应打印计数,因为此时您可能会中断循环:

while (true) {
    int dice1 = getRandom(1,6);
    int dice2 = getRandom(1,6);

    if (dice1 != dice2) {
        count++;
    } else {
        break;
    }
}
System.out.println(count);

你总是在打印计数。只有当数字相等时,才应打印计数,因为此时您可能会中断循环:

while (true) {
    int dice1 = getRandom(1,6);
    int dice2 = getRandom(1,6);

    if (dice1 != dice2) {
        count++;
    } else {
        break;
    }
}
System.out.println(count);
你快到了

您面临的问题是,您永远不会终止
while循环。您将需要以下内容:

int count = 0;
boolean roll = true;
while (roll) {
    int dice1 = getRandom(1,6);
    int dice2 = getRandom(1,6);

    if (dice1 != dice2) {
        count ++;
    } else {
       roll = false;
}
System.out.println(count);
我添加了一个
布尔值
,它表示是否应该掷骰子。一旦两个骰子的值相等,
roll
设置为false,循环终止。

您就快到了

import java.util.Scanner;
public class Dice
{

public static void main(String[] args)
{
        Scanner myInput=new Scanner(System.in);
        System.out.print("Tell me how many rolls of dice you would like to see");
        int numRolls=myInput.nextInt();

        //create array based on rolls
        int [] diceArray=new int[13];
        int diceRoll1;
        int diceRoll2;
        for(int index=0;index < numRolls;index++)
        {
            //diceArray[index]=(int)(Math.random()*11)+2;
            diceRoll1=(int)(Math.random()*6)+1;
            diceRoll2=(int)(Math.random()*6)+1;
            diceArray[diceRoll1]++;
        }
        for(int index=2;index<diceArray.length;index++)
        {
            //System.out.println(diceArray[index]);
            System.out.println(index + " " + diceArray[diceRoll1]);
        }
}

}
您面临的问题是,您永远不会终止
while循环。您将需要以下内容:

int count = 0;
boolean roll = true;
while (roll) {
    int dice1 = getRandom(1,6);
    int dice2 = getRandom(1,6);

    if (dice1 != dice2) {
        count ++;
    } else {
       roll = false;
}
System.out.println(count);
我添加了一个
布尔值
,它表示是否应该掷骰子。一旦两个骰子的值相等,
roll
设置为false,循环终止。

import java.util.Scanner;
import java.util.Scanner;
public class Dice
{

public static void main(String[] args)
{
        Scanner myInput=new Scanner(System.in);
        System.out.print("Tell me how many rolls of dice you would like to see");
        int numRolls=myInput.nextInt();

        //create array based on rolls
        int [] diceArray=new int[13];
        int diceRoll1;
        int diceRoll2;
        for(int index=0;index < numRolls;index++)
        {
            //diceArray[index]=(int)(Math.random()*11)+2;
            diceRoll1=(int)(Math.random()*6)+1;
            diceRoll2=(int)(Math.random()*6)+1;
            diceArray[diceRoll1]++;
        }
        for(int index=2;index<diceArray.length;index++)
        {
            //System.out.println(diceArray[index]);
            System.out.println(index + " " + diceArray[diceRoll1]);
        }
}

}
公共类骰子 { 公共静态void main(字符串[]args) { 扫描仪myInput=新扫描仪(System.in); System.out.print(“告诉我你想看到多少骰子”); int numRolls=myInput.nextInt(); //基于卷创建数组 int[]数组=新的int[13]; int-1; int-2; for(int index=0;indeximport java.util.Scanner; 公共类骰子 { 公共静态void main(字符串[]args) { 扫描仪myInput=新扫描仪(System.in); System.out.print(“告诉我你想看到多少骰子”); int numRolls=myInput.nextInt(); //基于卷创建数组 int[]数组=新的int[13]; int-1; int-2; for(int index=0;index对于(int index=2;index>while
循环,你从何处中断?在我看来,这就像
while(getRandom(1,6)!=getRandom(1,6))count++;
应该完成这项工作。你可以使用:1+new Random()代替getRandom函数。nextInt(6);此外,您还可以阅读更多有关while的内容。您在哪里中断
while
循环?在我看来,这就像
while(getRandom(1,6)!=getRandom(1,6))count++;
应该完成这项工作。您可以使用:1+new Random()代替getRandom函数。nextInt(6);您还可以阅读更多关于do的信息while@GoswinvonBrederlow实现相同行为有多种方法。您也可以使用
do{}while()
。@GoswinvonBrederlow实现相同行为有多种方法。您也可以使用
do{}while()