首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >跟踪制胜模具

跟踪制胜模具
EN

Stack Overflow用户
提问于 2018-09-14 18:01:00
回答 2查看 600关注 0票数 2

所以这就是我想做的。程序应该有一个循环,迭代10次。每次循环迭代时,它都应该同时滚动两个骰子。价值最高的模具获胜。在打成平手的情况下,骰子中的某一卷是没有赢家的。

当循环迭代时,程序应该:

  1. 询问用户是否准备好滚动。
  2. 显示滚动的结果,用户的滚动数,计算机的滚动,以及结果(谁赢,谁输,谁平局)。
  3. 计算计算机获胜的次数。
  4. 统计用户获胜的次数

模具目标代码:

代码语言:javascript
复制
import java.util.Random;

/**
   The Die class simulates a six-sided die.
*/

public class Die
{
   private int sides;   // Number of sides
   private int value;   // The die's value

  /**
     The constructor performs an initial
     roll of the die.
     @param numSides The number of sides for this die.
  */

  public Die(int numSides)
  {
     sides = numSides;
     roll();
  }

  /**
     The roll method simulates the rolling of
     the die.
  */

  public void roll()
  {
     // Create a Random object.
     Random rand = new Random();

     // Get a random value for the die.
     value = rand.nextInt(sides) + 1;
  }

  /**
     getSides method
     @return The number of sides for this die.
  */

  public int getSides()
  {
     return sides;
  }

  /**
     getValue method
     @return The value of the die.
  */

  public int getValue()
  {
     return value;
  }
}

这是使用目标代码的代码,用于骰子及其移动。

代码语言:javascript
复制
public class MitchellLab06
 {
    public static void main(String[] args)
    {
        final int DIE1_SIDES = 6; //Number of sides for die #1
        final int DIE2_SIDES = 6; //Number of sides for die #1
        final int MAX_ROLLS = 10; //Number of ties to roll

        // Create two instances of the Die class.
        Die die1 = new Die(DIE1_SIDES);
        Die die2 = new Die(DIE2_SIDES);

        //Display the initial value of the dice.
        System.out.println("This program simulates the rolling of a " + 
        DIE1_SIDES + " sided die and another " +
        DIE2_SIDES + " sided die.");

        System.out.println("The initial value of the dice:");
        System.out.println(die1.getValue() + " " + die2.getValue());

        //Roll the dice 10 times.
        System.out.println("Rolling the dice " + MAX_ROLLS + " times");

        for(int i = 0; i < MAX_ROLLS; i++)
        {
            //Roll the dice.
            die1.roll();
            die2.roll();

            //Display the value of the dice.
            System.out.println(die1.getValue() + " " + die2.getValue());
        }
    }
}

我需要帮助跟踪哪一个骰子获胜,从10个滚动和确定用户是否赢,计算机赢,或这是一个平分。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-09-14 18:29:44

一种简单而又优雅的解决方案,它使用一个简单的while循环和一对变量来保存用户评分、comp分数以及每个变量的总赢数。

代码语言:javascript
复制
int userWin = 0, compWin = 0;
int MAX_ATTEMPTS = 10;

while(MAX_ATTEMPTS > 0) {

    int userScore = 0, compScore = 0;
    //Roll the dice for user
    die1.roll();
    die2.roll();
    userScore = die1.getValue() + die2.getValue();

    //Roll the dice for comp
    die1.roll();
    die2.roll();
    compScore = die1.getValue() + die2.getValue();       

    // determine winner
    if (userScore > compScore) {
        System.out.println("User wins! \nUser score = " + userScore + ", Comp score = " + compScore);            
        userWin++;
    }
    else if (userScore < compScore) {
        System.out.println("Comp wins! \nUser score = " + userScore + ", Comp score = " + compScore);
        compWin++;
    } else {
        System.out.println("Draw!\nUser score = " + userScore + ", Comp score = " + compScore);
    }

    MAX_ATTEMPTS --;

}
System.out.println("User won = " + userWin + " times! ");  
System.out.println("Comp won = " + compWin + " times! ");
票数 0
EN

Stack Overflow用户

发布于 2018-09-14 18:09:03

一个示例解决方案是初始化两个数组,一个用于计算机,一个用于用户。每次抛出骰子时,都会在位置上增加数组,用骰子抛出转弯号。

代码语言:javascript
复制
int [] computer = new int[10];
int [] user = new int [10]; 

for (int i=0;i<10; ++i) {
   int diceUser = throwDice();
   int diceComputer = throwDice();
   if (diceUser> diceComputer) {
      user[i] = diceUser;
   }
   else if (diceUSer<diceComputer) {
      computer[i]= diceComputer;
   }
   else {
      computer[i] = diceComputer;
      user[i] = diceUser;
   }     
}

每次计算机或用户丢失时,数组中都会有0。当它是抽签时,两个数组将在相同的索引中包含相同的值。

数组的索引是在转弯之后。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52337139

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档