C ++在循环中/提示时生成新随机数的问题

时间:2018-05-27 02:58:44

标签: c++ loops random srand

我差不多完成了这个基本的骰子程序,但是很难找到我做错了什么。它应该为用户随机滚动2个骰子,并且他们可以选择保持它们滚动(K)或再次滚动(R)。我希望程序输出新的"随机"每次用户输入" R"当被问及他们是否想要再次滚动时,相同的数字仍在显示。

该程序的其余部分似乎运行良好 - 用户对抗计算机。当用户输入时,如何为roll1和roll2生成新数字" R"当被要求保留或重新滚动时?

#include <stdlib.h> /// need this for srand() -- for random numbers
#include <time.h> /// need this for time() -- time
#include <iostream>  /// need this for cout<< and cin>>
using namespace std; /// need this for cout<< and cin>>

int main()
{
    int iseed = (int)time(0);
    srand(iseed);

    cout << "Beat the computer! \n";

    int roll1 = 1+rand()%6; /// make a random number for die # 1 for user
    int roll2 = 1+rand()%6; /// make a random number for die # 2 for user
    int UserRoll = roll1 + roll2; /// totals the sum of die 1 and die 2 for the user
    char keep;

    do
    {
    cout << "You rolled a " << roll1 << " and a " << roll2 << " for a total of: " << UserRoll << "\n";
    cout << "\n";

        do
        {
        cout << "Would you like to keep this total, or roll again? \n";
        cout << "\n";
        cout << "Enter \"K\" for keep and \"R\" for roll again: \n";
        cin >> keep;

            if (keep != 'K' && keep != 'R')

            {
                cout << "That is not a valid choice. Please choose Y to keep your total or N to roll again. " << endl;
                cout << "\n";
            }

            } while(keep != 'K' && keep != 'R');



            if (keep == 'R')
            {
                cout << "You chose R--let's roll again. \n";
            }


            else
            {
            cout << "Great! Your total is " << UserRoll << "\n";
            }
        } while (keep == 'R');

    int roll3 = 1+rand()%6; /// make a random number for die # 1 for computer
    int roll4 = 1+rand()%6; /// make a random number for die # 2 for computer
    int ComputerRoll = roll3 + roll4; /// totals the sum of die 1 and die 2 for the computer


    cout << "The computer rolled a " << roll3 << " and a " << roll4 << " for a total of: " << ComputerRoll << "\n";
    cout << "\n";

    if (ComputerRoll < UserRoll)
    {
        cout << "Congratulations! You won! \n";
    }

    if (ComputerRoll > UserRoll)
    {
        cout << "Sorry. You lose. \n";
    }

    if (ComputerRoll == UserRoll)
    {
        cout << "It's a tie. \n";
    }

return 0;
}

/*
SAMPLE RUNS:
------------
Beat the computer!
You rolled a 4 and a 6 for a total of: 10

Would you like to keep this total, or roll again?

Enter "K" for keep and "R" for roll again:
R
You chose R--let's roll again.
You rolled a 4 and a 6 for a total of: 10

Would you like to keep this total, or roll again?

Enter "K" for keep and "R" for roll again:
K
Great! Your total is 10
The computer rolled a 4 and a 6 for a total of: 10

It's a tie.

Process returned 0 (0x0)   execution time : 8.763 s
Press any key to continue.

*/

2 个答案:

答案 0 :(得分:3)

您只是滚动一次并再次使用该滚动的结果。如果用户再次按下R键。它应该再次滚动以更新新数字。为此,只需添加

void roll(){
    roll1 = 1+rand()%6; /// make a random number for die # 1 for user
   roll2 = 1+rand()%6; /// make a random number for die # 2 for user
UserRoll = roll1 + roll2; /// totals the sum of die 1 and die 2 for the user

}
  

请记住:roll1,roll2和UserRoll应在roll()函数

之前全局声明

在main()方法之前,当用户按下R时调用它 之后

cout << "Beat the computer! \n";

并从main方法中删除它。 这就是你需要做的所有事情

答案 1 :(得分:0)

大!我终于有了一个工作计划!如果你认为有一种方法可以使这个更整洁,更容易阅读,任何人都会告诉我:

primarySwatch