用JAVA模拟掷骰子游戏

用JAVA模拟掷骰子游戏,java,Java,我是Java新手,试图通过在网上找到的一些练习来学习。如果这太天真了,请原谅 本练习旨在编写一个符合以下规则的骰子游戏程序: 在掷骰子游戏中,通过线下注如下:两个六边骰子 翻滚;掷骰子回合中的第一轮骰子被称为“出来掷骰子” 7或11的出场掷骰自动获胜,2、3或12的出场掷骰自动获胜 自动丢失。如果4、5、6、8、9或10在输出卷上滚动,则该数字 变为“点数”。玩家继续掷骰子,直到7点或点数结束 翻滚。如果先掷点数,则玩家赢得赌注。如果先掷7, 然后玩家输了。 编写一个程序,在没有人工干预的情况下

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

本练习旨在编写一个符合以下规则的骰子游戏程序:

在掷骰子游戏中,通过线下注如下:两个六边骰子 翻滚;掷骰子回合中的第一轮骰子被称为“出来掷骰子” 7或11的出场掷骰自动获胜,2、3或12的出场掷骰自动获胜 自动丢失。如果4、5、6、8、9或10在输出卷上滚动,则该数字 变为“点数”。玩家继续掷骰子,直到7点或点数结束 翻滚。如果先掷点数,则玩家赢得赌注。如果先掷7, 然后玩家输了。 编写一个程序,在没有人工干预的情况下,使用这些规则模拟一个垃圾游戏 输入。与其要求下注,程序应该计算 玩家会赢或输。该程序应模拟滚动两个骰子和 计算总数。添加一个循环,使程序可以玩10000个游戏。添加 计算玩家赢了多少次,以及玩家赢了多少次的计数器 玩家输了。在10000场比赛结束时,计算获胜的概率 [即,赢/(赢+输)]并输出该值。从长远来看,谁 你会赢得最多的比赛,还是众议院

以下是我编写的代码:

// 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;
}
//掷骰子游戏
公共静态void main(字符串[]args)
{
int-1=0;
int=2=0;
int scorenew=0;
int点=0;
int=0;
整数损失=0;
对于(int i=0;i<10000;i++)
{
System.out.println(“掷骰子”);
积分=掷骰子(骰子1,骰子2);
System.out.println(“\n分数”+分数);
如果(分数=11 | |分数=7)
{
System.out.println(“\n Score=“+Score”);
System.out.println(“您赢了”);
wins=wins+1;
}
如果(分数=2 | |分数=3 | |分数=12)
{
System.out.println(“\n Score=“+Score”);
System.out.println(“您输了”);
损失=损失+1;
}
否则如果(分数==4 | |分数==5 | |分数==6 | |分数==8 | |分数==9 | |分数==10)
{
分数=分数+分数;
System.out.println(“\n Point=“+Point”);
做
{
scorenew=掷骰子(骰子1,骰子2);
System.out.println(“\n Score new=“+scorenew”);
如果(scorenew==点)
{
System.out.println(“\n您赢了”);
wins=wins+1;
点=0;
打破
}
如果(scorenew==7)
{
System.out.println(“\n您输了”);
点=0;
损失=损失+1;
打破
}
}while(scorenew!=点| | scorenew!=7);
}
}
System.out.println(“\n wins数=“+wins
+“和损失数量=”+损失+
“以及赢得一场比赛的概率=”+(双)赢/(赢+输));
}
公共静态整数滚动(整数d1,整数d2)
{
Random randomGenerator=新的Random();
int dice1=randomGenerator.nextInt(6)+1;
int dice2=randomGenerator.nextInt(6)+1;
System.out.println(“\n dice1=“+dice1+”dice2=“+dice2”);
智力分数=骰子1+骰子2;
返回分数;
}
每次我运行代码时,先执行do while条件,所以有人能帮我找出哪里出了问题吗

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

此条件始终为真,因此有一个无限循环。另外,为什么要将
d1
d2
传递到
roll()
函数中?它们是完全未使用和不需要的。

边做边做正是你希望它做的事情。首先执行主体,然后计算条件以查看是否应再次运行。实际上,您不需要执行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;
    }
  }
}
就像@Lee Daniel Crocker说的,你不需要将骰子1和骰子2传递给掷骰功能

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;
}
另一个可能有用的方法是不要在方法的顶部声明所有变量。您不需要第三个条件之外的scorenew或point,事实上,您根本不需要scorenew,因为您有point:

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() {
  ...
}
publicstaticvoidmain(字符串[]args)抛出java.lang.Exception{
int=0;
整数损失=0;
对于(int i=0;i<10000;i++){
System.out.println(“掷骰子”);
积分=滚动();
System.out.println(“\n分数”+分数);
如果(分数=7 | |分数=11){
System.out.println(“\n Score=“+Score”);
System.out.println(“你赢了”);
wins=wins+1;
}否则如果(分数=2 | |分数=3 | |分数=12){
System.out.println(“\n Score=“+Score”);
System.out.println(“你输了”);
损失=损失+1;
}否则{
积分=分数;
System.out.println(“\n Point=“+Point”);
while(true){
分数=滚动();
System.out.println(“\n Score new=“+Score”);
如果(分数==分){
System.out.println(“\n您赢了”);
wins=wins+1;
打破
}
如果(分数=7){
System.out.println(“\n您输了”);
损失=损失+1;
打破
}
}
}
}
System.out.println(“\n wins number=“+wins
+“和损失数量=”+损失+
“以及赢得一场比赛的概率=”+(双)赢/(赢+输));
}
公共静态int roll(){
...
}

这里没有无限循环。那么告诉我-
scorenew
point
的值是多少?这个表达式是假的吗?
scorenew==7
point!=7
。带中断的条件永远不会让这些值被计算,但它们也会中断,因此不存在无限循环。可以是while(true)。不,对于这些值,表达式仍然为true,因为前半部分为true。这是我的观点——这是一段时间(真的)。事实上有一个条件