【发布时间】:2013-07-10 05:51:33
【问题描述】:
我一天前开始使用 C++,但在尝试创建石头、剪子布游戏时遇到了问题。这段代码并不是完成后的样子,但我这样做是为了演示我的问题。
我已经做了 brps,意思是:bot 石头剪刀布,一个 1-3 的随机数,其中对应的数字会产生一个 cout,说明 bot 选择了什么项目。
代码的 rand 部分是通过查看不同的论坛和对以前问题的答案制作的,但我似乎无法解决这个问题。每当我运行程序时,无论我做什么,它都会显示“Bot 选择了 the rock”。但是,如果我删除 if 语句,并简单地打印 brps 它每次都会显示一个随机数。所以我需要帮助来弄清楚为什么程序每次都选择岩石,而该选择应该由 rand 选择的数字来定义。
请随意评论代码的其他部分,因为我预计这一切都写得有些糟糕:L
编辑:urps 是用户输入答案的地方。在这个例子中我没有使用它。
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main()
{
cout<<"Hi! Welcome to ROCK PAPER SCISSORS!\n";
cout<<"To play, press enter.\n";
cin.get();
system("cls");
int game();
{
srand(time(NULL));
int brps = rand()>>4, urps;
brps = brps % 3 + 1;
cout<<"Bot chose ";
if (brps = 1){
cout<<"the rock.\n";}
else if (brps = 2){
cout<<"the paper.\n"; }
else if (brps = 3){
cout<<"the scissors.\n"; }
else{
cout<<"invalid.\n.";}
cin.get();
}
}
【问题讨论】:
标签: c++ if-statement random