【问题标题】:single dice roll simulation with C++使用 C++ 进行单掷骰子模拟
【发布时间】:2017-10-08 00:03:49
【问题描述】:

我正在编写一个程序来模拟具有随机函数的掷骰子。 这是我的代码,但我有无限循环。 该程序应该询问用户他们想掷骰子多少次。 然后我使用 for 循环将骰子从 1 掷到 6 我把所有这些都放在了一个do-while中,以便只允许用户在 1 和 6 之间进行选择,如果选择在 1 到 6 之外,则应该说这是一个无效的选择。 我不允许在此使用函数或数组

#include<iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
    //i is the counter for the loop
    int i = 0; 
    //store total count for landing number
    int num1 =0;
    int num2 =0;
    int num3 =0;
    int num4 =0;
    int num5 =0;
    int num6 =0;

    int Num1 =0;
    int Num2 =0;
    int Num3 =0;
    int Num4 =0;
    int Num5 =0;
    int Num6 =0;
    int randNum;
    int times;


    //User selection
    int selection;


    /* initialize random seed: */
   srand ((unsigned int)time(NULL));

   /* generate random number: */
   randNum = rand() % 6 + 1;

    cout<<"How many times would you like to roll the dice? " << endl;
    cin >> selection;

   do{
        for(i = 1; i <= selection; i++)
        {

            if(randNum==1)
            {
                num1++;
            }
            else if(randNum==2)
            {
                num2++;
            }
            else if(randNum==3)
            {
                num3++;
            }
            else if(randNum==4)
            {
                num4++;
            }
            else if(randNum==5)
            {
                num5++;
            }
            else if(randNum==6)
            {
                num6++;
            }
        }
    Num1 = num1/times *100;
    Num2 = num2/times *100;
    Num3 = num3/times *100;
    Num4 = num4/times *100;
    Num5 = num5/times *100;
    Num6 = num6/times *100;


    cout <<"# Rolled \t # Times \t % Times" << endl;
    cout <<"----- \t ------- \t -------" << endl;
    cout <<" 1 \t " << num1 << "\t " << fixed << setprecision(2) << Num1 << "%\n";
    cout <<" 2 \t " << num2 << "\t " << fixed << setprecision(2) << Num2 << "%\n";
    cout <<" 3 \t " << num3 << "\t " << fixed << setprecision(2) << Num3 << "%\n";
    cout <<" 4 \t " << num4 << "\t " << fixed << setprecision(2) << Num4 << "%\n";
    cout <<" 5 \t " << num5 << "\t " << fixed << setprecision(2) << Num5 << "%\n";
    cout <<" 6 \t " << num6 << "\t " << fixed << setprecision(2) << Num6 << "%\n";
    }

   while(i >= 1 || i <= 6);
   {
       cout << "This is an invalid number. \n"
        << "The number of rolls should be equal to or greater than 1.\n"
        << "Please enter again.\n";
   }
}

【问题讨论】:

  • 似乎您想在执行所有其他工作和输出之前检查有效输入。您可能会考虑使用数组进行计数,这将是更简单的代码。 times 在您使用时未初始化,整数数学会给您错误的值。您也可以将掷骰子放在 for 循环中。
  • 当您使用调试器执行代码时,一次一行,代码执行“无限循环”的原因是什么?
  • 您好,我不允许对这段代码使用数组或函数
  • 你可能想在循环中生成随机数。
  • “我不允许使用数组或函数” - 然后从其他来源学习编程。

标签: c++ random


【解决方案1】:

你的条件语句:i = 1; i &lt;= selection; i++ 应该在你有的时候进入决赛。

试试:

do {
    if (randNum==1) {
        num1++;
    } else if(randNum==2) {
        num2++;
    } else if(randNum==3) {
        num3++;
    } else if(randNum==4) {
        num4++;
    } else if(randNum==5) {
        num5++;
    } else if(randNum==6) {
       num6++;
    }
    i += 1;
} while (i <= selection);

【讨论】:

    【解决方案2】:

    无限循环是do - while...

    我不确定你在那里尝试做什么,但在你的测试中你总是会得到一个大于 1 或小于 6 的数字 - 你不能同时失败...

    【讨论】:

      猜你喜欢
      • 2019-03-09
      • 2016-04-18
      • 2011-07-12
      • 2018-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多