<分区>
通过 C++ 实践介绍 CS。
我无法弄清楚如何将玩家 1 和玩家 2 获得的随机数分配给字符串值“p1_string_val”和“p2_string_val”。我在下面显示了到目前为止的代码。
有人可以帮我弄清楚如何将随机数分配给字符串吗?任何进一步的帮助也将受到欢迎!太感谢了。
分配说明: 我已完成第 0 步和第 1 步。我需要帮助将随机数分配给字符串,任何其他帮助都会有所帮助。
我的代码如下所示:
"玩家一有石头/布/剪刀
玩家二有石头/布/剪刀
请按任意键继续”
- 根据P1_int_val 和P2_int_val 中存储的数值,使用if、else 和else if 语句将以下字符串存储到P1_string_val、P2_string_val 中。
int_val 0 string_val 岩石
int_val 1 string_val 纸
int_val 2 string_val 剪刀
#include <fstream>
#include <iostream>
#include <string>
#include <time.h>
using namespace std;
void main()
{
int p1_int_val, p2_int_val;
string p1_string_val, p2_string_val;
srand(time(NULL));
p1_int_val = (rand() % 3);
p2_int_val = (rand() % 3);
//to check the random integer are between 0-2
/*cout << "Player one has this " << p1_int_val << endl;
cout << "Player two has this " << p2_int_val << endl; */
if (p1_int_val == 0)
cout << "Player one has rock " << endl;
if (p1_int_val == 1)
cout << "Player one has paper" << endl;
else if (p1_int_val == 2)
cout << "Player one has scissors" << endl;
if (p2_int_val == 0)
cout << "Player two has rock " << endl;
if (p2_int_val == 1)
cout << "Player two has paper" << endl;
else if (p2_int_val == 2)
cout << "Player two has scissors" << endl;
return 0;
}
Once the program has been completed it should display:
//Player one has rock/paper/scissors (depends on what number they got)
//Player two has rock/paper/scissors
//Player one or two has won OR TIE