java 用JAVA模拟掷骰子游戏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29439838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 15:18:19  来源:igfitidea点击:

Simulating Game of Craps in JAVA

java

提问by Dhrubojyoti Bhattacharjee

I am new to Java and was trying to learn by doing some excercises that I found online. So please excuse me if this is too naive.

我是 Java 新手,并试图通过做一些我在网上找到的练习来学习。所以如果这太天真了,请见谅。

This excercise was about writing a program for game of craps with the following rules:

这个练习是关于编写一个具有以下规则的掷骰子游戏程序:

In the game of craps, a pass line bet proceeds as follows: Two six-sided dice are rolled; the first roll of the dice in a craps round is called the “come out roll.” A come out roll of 7 or 11 automatically wins, and a come out roll of 2, 3, or 12 automatically loses. If 4, 5, 6, 8, 9, or 10 is rolled on the come out roll, that number becomes “the point.” The player keeps rolling the dice until either 7 or the point is rolled. If the point is rolled first, then the player wins the bet. If a 7 is rolled first, then the player loses. Write a program that simulates a game of craps using these rules without human input. Instead of asking for a wager, the program should calculate whether the player would win or lose. The program should simulate rolling the two dice and calculate the sum. Add a loop so that the program plays 10,000 games. Add c ounters that count how many times the player wins, and how many times the player loses. At the end of the 10,000 games, compute the probability of winning [i.e., Wins / (Wins + Losses)] and output this value. Over the long run, who is going to win the most games, you or the house?

在掷骰子游戏中,通过线下注的过程如下: 掷出两个六面骰子;掷骰子的第一次掷骰子称为“掷骰子”。掷出 7 或 11 自动获胜,掷出 2、3 或 12 自动失败。如果在出局的掷骰子上掷出 4、5、6、8、9 或 10,则该数字成为“点数”。玩家不断掷骰子,直到掷出 7 或点数。如果该点首先被掷出,则玩家赢得赌注。如果先掷出 7,则玩家输。编写一个程序,在没有人工输入的情况下使用这些规则模拟掷骰子游戏。程序应该计算玩家是赢还是输,而不是要求下注。程序应该模拟掷两个骰子并计算总和。添加一个循环,让程序播放 10,000场比赛。添加计算玩家获胜次数和玩家失败次数的计数器。在 10,000 场比赛结束时,计算获胜概率 [即 Wins / (Wins + Losses)] 并输出该值。从长远来看,谁将赢得最多的比赛,你还是房子?

Here is the code that I have written :

这是我写的代码:

// GAME OF CRAPS
public static void main (String[] args)
{
  int dice1 = 0;
  int dice2 = 0;
  int scorenew = 0;
  int point = 0;
  int wins = 0;
  int loss = 0;

  for (int i = 0; i < 10000; i++)
  {
    System.out.println ("roll the dices");
    int score = roll (dice1, dice2);
    System.out.println ("\n score " + score);

    if (score == 11 || score == 7)
    {
      System.out.println ("\n Score = " + score);
      System.out.println ("you win");
      wins = wins + 1;
    }
    if (score == 2 || score == 3 || score == 12)
    {
      System.out.println ("\n Score = " + score);
      System.out.println ("you lose");
      loss = loss + 1;
    }
    else if (score == 4 || score == 5 || score == 6 || score == 8 || score == 9 || score == 10)
    {
      point = point + score;
      System.out.println ("\n Point = " + point);

      do
      {
        scorenew = roll (dice1, dice2);
        System.out.println ("\n Score new = " + scorenew);
        if (scorenew == point)
        {
          System.out.println ("\n you win");
          wins = wins + 1;
          point = 0;
          break;
        }
        if (scorenew == 7)
        {
          System.out.println ("\n you lose");
          point = 0;
          loss = loss + 1;
          break;
        }
      } while (scorenew != point || scorenew != 7);

    }
  }

  System.out.println ("\n number of wins = " + wins
    + " and number of loss = " + loss +
    " and the probability for winning a game = " + (double) wins / (wins + loss));
}

public static int roll (int d1, int d2)
{
  Random randomGenerator = new Random ();
  int dice1 = randomGenerator.nextInt (6) + 1;
  int dice2 = randomGenerator.nextInt (6) + 1;

  System.out.println ("\n dice1 = " + dice1 + " dice2 = " + dice2);

  int score = dice1 + dice2;
  return score;
}

Everytime I run the code the do-while condition gets executed first, so please can anyone help me figure out where I am going wrong?

每次我运行代码时,do-while 条件都会先执行,所以请谁能帮我找出我哪里出错了?

回答by Lee Daniel Crocker

do {
} while(scorenew!=point || scorenew != 7);

This condition is alwaystrue, so you have an infinite loop. Also, why do you pass d1and d2into the roll()function? They are completely unused and unneeded.

这个条件总是成立的,所以你有一个无限循环。另外,为什么要传递d1d2进入roll()函数?它们完全没有使用和不需要。

回答by DTing

Do while is doing exactly what it you should expect it to do. Executes the body first then evaluates the conditional to see if it should run again. You don't actually need a do while though, you want to run until one of the conditions breaks you out of the while loop.

Do while 就是做你应该期望它做的事情。首先执行主体然后评估条件以查看它是否应该再次运行。不过,您实际上并不需要 do while,您希望一直运行,直到其中一个条件使您脱离 while 循环。

else {
  point = score;
  System.out.println ("\n Point = " + point);

  while (true) {
    scorenew = roll (dice1, dice2);
    System.out.println ("\n Score new = " + scorenew);
    if (scorenew == point) {
      System.out.println ("\n you win");
      wins = wins + 1;
      break;
    }
    if (scorenew == 7) {
      System.out.println ("\n you lose");
      loss = loss + 1;
      break;
    }
  }
}

Like @Lee Daniel Crocker said you don't need to pass in dice1 and dice2 to the roll function.

就像@Lee Daniel Crocker 说的,您不需要将 dice1 和 dice2 传递给 roll 函数。

public static int roll() {
  Random randomGenerator = new Random();
  int dice1 = randomGenerator.nextInt(6) + 1;
  int dice2 = randomGenerator.nextInt(6) + 1;
  System.out.println("\n dice1 = " + dice1 + " dice2 = " + dice2);
  return dice1 + dice2;
}

Another thing that might help is not declaring all the variables at the top of your method. You don't need scorenew or point outside of the third condition, in fact you don't need scorenew at all since you have point:

另一件可能有帮助的事情是不要在方法顶部声明所有变量。您不需要 scorenew 或第三个条件之外的点,实际上您根本不需要 scorenew ,因为您有点:

public static void main(String[] args) throws java.lang.Exception {
  int wins = 0;
  int loss = 0;

  for (int i = 0; i < 10000; i++) {
    System.out.println("roll the dices");
    int score = roll();
    System.out.println("\n score " + score);

    if (score == 7 || score == 11) {
      System.out.println("\n Score = " + score);
      System.out.println("you win");
      wins = wins + 1;
    } else if (score == 2 || score == 3 || score == 12) {
      System.out.println("\n Score = " + score);
      System.out.println("you lose");
      loss = loss + 1;
    } else {
      int point = score;
      System.out.println("\n Point = " + point);
      while (true) {
        score = roll();
        System.out.println("\n Score new = " + score);
        if (score == point) {
          System.out.println("\n you win");
          wins = wins + 1;
          break;
        }
        if (score == 7) {
          System.out.println("\n you lose");
          loss = loss + 1;
          break;
        }
      }
    }
  }

  System.out.println("\n number of wins = " + wins
      + " and number of loss = " + loss +
      " and the probability for winning a game = " + (double) wins / (wins + loss));
}

public static int roll() {
  ...
}