【问题标题】:How to obtain the number of dice rolls it takes to roll a certain number?如何获得掷出一定数量的骰子数量?
【发布时间】:2023-03-15 05:30:01
【问题描述】:

方向:继续“掷骰子”直到总共达到 7 个,并记录要达到 7 个需要掷多少次。打印出每次试验的卷数。

每次试玩后,卷数都一样,不知道为什么。

    import java.util.*;
  public class GeometricDistribution {
  public static void main (String [] args) {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Please enter a positive integer.");
    int input = 0;
    //loop if the user doesn't enter an actual integer
    while (!keyboard.hasNextInt()) {
      System.out.println("Enter an integer value, please.");
      keyboard.nextLine();  //remove everything the user input 
    }
    input = keyboard.nextInt();
    while (input <= 0) {
      System.out.println("Please enter a POSITIVE integer.");
      input = keyboard.nextInt();
    }
    int sum = 0;
    int numRolls = 0;
    for (int i = 1; i <= input; i++) {
      while ((sum) != 7) {
        numRolls++;
        int die1 = (int)(Math.random()*6+1);
        int die2 = (int)(Math.random()*6+1);
        sum = die1 + die2;
      }
      System.out.println("You rolled a 7 on roll #" + numRolls);
    }           
  }
}

编辑:这是我的代码现在的样子:

    int sum;
int numRolls = 0;
int sumRolls = 0;
for (int i = 1; i <= input; i++) {
  sum = 0; 
  while ((sum) != 7) {
    numRolls++;
    int die1 = (int)(Math.random()*6+1);
    int die2 = (int)(Math.random()*6+1);
    sum = die1 + die2;
  }
  System.out.println("You rolled a 7 on roll #" + numRolls);
  sumRolls += numRolls;
}
System.out.println("Average number of rolls to get a 7: " + (sumRolls/input));

看起来对吗?

编辑:

这是我为这个项目提交给教授的最终代码。明确、完整的方向如下:

写一个程序运行一个简单的概率实验一共N次 试验(其中 N 是用户输入的正整数)。每个 进行实验,模拟两个六面骰子的滚动 使用随机数并计算总数。继续“掷骰子” 直到总共达到七个,并跟踪它的滚动次数 需要达到七。打印每个卷的数量 审判。例如,在第一次试验中,它可能需要五次滚动 在看到七之前(七出现在第五卷上)然后 第二次试验可能需要八次滚动,直到七次,依此类推。 在程序结束时打印出它的平均卷数 在 N 次试验中获得了 7 分。笔记: + 你的程序应该检查用户输入的 N 是一个正整数。它应该重新提示用户,直到他们输入一个肯定的 如果他们输入 0 或负数,则为整数。 (如果用户输入 非整数,可以让程序崩溃) + 最简单的方法可能是对嵌套在 for 循环中的每个试验使用一个 while 循环,该循环将试验总数计算到 ñ + 您将需要跟踪每次试验所花费的滚动数的总和,以便能够在所有试验后计算平均值 审判已经结束 + 这个程序模拟几何概率分布......随着 N 增加平均试验次数达到 7 应该接近 6 (因为有 1/6 的机会掷出 7)

我的回答:

import java.util.*;
public class HW9 {
  public static void main (String [] args) {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Please enter a positive integer.");
    int input = 0;
    //loop if the user doesn't enter an actual integer
    while (!keyboard.hasNextInt()) {
      System.out.println("Enter an integer value, please.");
      keyboard.nextLine();  //remove everything the user input 
    }
    input = keyboard.nextInt();
    while (input <= 0) {
      System.out.println("Please enter a POSITIVE integer.");
      input = keyboard.nextInt();
    }
    int sumRolls = 0;
    for (int i = 1; i <= input; i++) {
      int sum = 0;
      int numRolls = 0;
      while ((sum) != 7) {
        int die1 = (int)(Math.random()*6+1);
        int die2 = (int)(Math.random()*6+1);
        sum = die1 + die2;
        numRolls++;
      }
      System.out.println("You rolled a 7 on roll #" + numRolls);
      sumRolls += numRolls;
    }
    System.out.println("Average number of rolls to get a 7: " + (sumRolls/input));
  }
}

请告诉我您对上述代码在效率和优雅方面的看法。

非常感谢任何建议。

【问题讨论】:

  • 你应该学习“do while”,比如“do { numRolls++; die1 = ...;sum = die1+die2;}while(sum != 7);”
  • 嘿 Mikkel,感谢您的洞察力。你能看看最新的编辑,让我知道你的想法吗?

标签: java


【解决方案1】:
for (int i = 1; i <= input; i++) {
  sum = 0; // add this
  while ((sum) != 7) {
    numRolls++;
    int die1 = (int)(Math.random()*6+1);
    int die2 = (int)(Math.random()*6+1);
    sum = die1 + die2;
  }
  System.out.println("You rolled a 7 on roll #" + numRolls);
}  

你需要重新启动 sum 每次迭代

【讨论】:

  • 查看编辑。关于重置总和的某些操作会导致计算 7 所需的平均滚动数的错误值。随着试验数量的增加,它应该接近 6。
【解决方案2】:

循环中的每次迭代都需要printf

    for (int i = 1; i <= input; i++) {
      while ((sum) != 7) {
        numRolls++;
        int die1 = (int)(Math.random()*6+1);
        int die2 = (int)(Math.random()*6+1);
        sum = die1 + die2;
        System.out.printf("You rolled a %d on roll #" + numRolls + "\n", sum);
      }

    } 

【讨论】:

  • 还没学过 printf 方法……会在 API 上研究一下。谢谢你的意见,哈米德。
  • @tharooj,所以你可以简单地将printf替换为System.out.println("You rolled a " + sum + "on roll #" + numRolls);
【解决方案3】:

您将通过以下方式计算 sum != 7 少一次:

do {
  numRolls++;
  int die1 = (int)(Math.random()*6+1);
  int die2 = (int)(Math.random()*6+1);
  sum = die1 + die2;
} while ((sum) != 7);

并且通过在评估之前计算总和,使编译器有更好的机会不使用任何内存进行总和。在 die2 之后类似的 sum 意味着也不为 die2 使用内存。因此,最后 3 行提供了获得最优化结果代码的更好机会。

更特殊的优化是:

do {
  ++numRolls;
  twodices = (int)(Math.random()*36; // Throws 2 dices at the same time
} while (twodices < 6); // 0 - 5 represents the 6 different throws giving 7.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-14
    • 2020-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-06
    • 2021-03-12
    相关资源
    最近更新 更多