- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
对于我的类(class)项目,我们要制作一个“石头剪刀布”游戏,使用一个函数来显示菜单和验证输入,以及一个函数来确定谁赢了。当我绕过第一个函数时,我的代码将编译并正常运行。但是,当我同时使用这两个函数并输入 1
、2
或 3
进行选择时,它会给我 -4195200
作为一个值。
有什么帮助吗?这是我的代码:
// This project will be a game of Rock, Paper, Scissors
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
// Function Prototypes
int choices();
int userselect (int, int);
int main()
{
//Variables and Seed
int selection;
char again;
unsigned seed = time(0);
srand(seed);
int compselect = (rand()%3)+1;
//Constants for the menu choices
const int ROCK = 1,
PAPER = 2,
SCISSORS = 3;
do
{
//Display the menu choices and validate
choices();
cout << "You picked " << selection << endl;
cout << "The computer picked " << compselect << endl;
//Display the results
userselect(selection, compselect);
//Replay loop
cout << "Do you want to play again? (Y/N)";
cin >> again;
}
while (again == 'Y' || again == 'y');
return 0;
}
//***********************************************************
// This function will display the menu choices and validate *
//***********************************************************
int choices()
{
int selection;
cout << "********************************\n";
cout << "* Please Select A Choice *\n";
cout << "* 1 - Rock *\n";
cout << "* 2 - Paper *\n";
cout << "* 3 - Scissors *\n";
cout << "********************************\n";
cin >> selection;
//Validate the selection
while (selection < 1 || selection > 3)
{
cout << "ERROR: Enter a valid choice.";
cin >> selection;
}
return selection;
}
//***********************************************************
// This function will display the menu choices and validate *
//***********************************************************
int userselect (int num1, int num2)
{
// If user enters Rock
if (num1 == 1)
{
if (num2 == 1)
{
cout << "You picked Rock \n";
cout << "The computer picked Rock \n";
cout << " TIE! \n";
}
else if (num2 == 2)
{
cout << "You picked Rock \n";
cout << "The computer picked Paper \n";
cout << "Paper beats Rock. YOU LOSE! \n";
}
else if (num2 == 3)
{
cout << "You picked Rock \n";
cout << "The computer picked Scissors \n";
cout << " Rock beats Scissors. YOU WIN! \n";
}
}
// If user enters Paper
if (num1 == 2)
{
if (num2 == 1)
{
cout << "You picked Paper \n";
cout << "The computer picked Rock \n";
cout << " Paper beats Rock. YOU WIN! \n";
}
else if (num2 == 2)
{
cout << "You picked Paper \n";
cout << "The computer picked Paper \n";
cout << " TIE! \n";
}
else if (num2 == 3)
{
cout << "You picked Paper \n";
cout << "The computer picked Scissors \n";
cout << " Scissors beats Paper. YOU LOSE! \n";
}
}
// If user enters Scissors
if (num1 == 3)
{
if (num2 == 1)
{
cout << "You picked Scissors \n";
cout << "The computer picked Rock \n";
cout << " Rock beats Scissors. YOU LOSE! \n";
}
else if (num2 == 2)
{
cout << "You picked Scissors \n";
cout << "The computer picked Paper \n";
cout << "Scissors beat Paper. YOU WIN! \n";
}
else if (num2 == 3)
{
cout << "You picked Scissors \n";
cout << "The computer picked Scissors \n";
cout << " TIE! \n";
}
}
}
最佳答案
你不是handling the newline that's still in the cin
input stream ,并且您没有对因此失败的 cin >> 选择
进行任何错误检查。因为它失败了,selection
没有被赋予任何新值,而且由于您没有初始化它,您得到这个垃圾号码 -4195200
。
如果您执行了一些错误检查,或者允许您在 while
循环中通过初始化 selection 完成其工作的错误检查,您就会看到这一点
到 0
:
int selection = 0;
在 main()
中,您也完全无法存储调用 choices()
的结果。请记住,choices()
中的变量 selection
是一个局部变量,与 main() 中的变量
,您永远不会为其分配值,在此代码中:selection
不同)
selection = choices();
关于c++ - 石头,布,剪刀,奇数输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17894500/
所以,我正在用 Python 制作这款游戏。问题是,在剪刀、布和石头中可以有不同的组合,例如..石头和布、石头和剪刀等等。那么,如果不执行大量 elif 语句,我将如何做到这一点。 import
我是 javascript 新手,正在开发一个简单的石头、剪刀、布、蜥蜴、斯波克游戏。我想让用户从 5 张图像中进行选择,而不是在提示中填写。有谁知道我该怎么做?警报和确认消息最终也应该是 或类似的内
我正在制作一个小剪刀石头布游戏,我以为我已经完美地计划了一切,但由于某种原因,当你尝试玩时什么也没有发生。 基本上,有三张图像,一 block 石头、一张纸和一些剪刀,然后你单击一个。使用我输入的代码
我试图学习python并以此开始,当我尝试运行它时,我一直收到语法错误。光标跳到def start部分的close结束处。我不确定语法错误来自哪里,因为我用语音标记了所有打印件 #! python3
我有点困惑如何创建一个字段,其中将显示您获胜或平局的次数。喜欢: | Wins: | 234 | | Looses:| 234 | | Draws: | 434 | 意思是,如果我和新闻报纸赢得了答
如何让玩家玩随机数量的比赛,然后以全部分数结束游戏? 我正在尝试做一个循环,让玩家玩多场比赛,并在最后询问是否还想玩。 public class RockPaperScissors { pub
每当我运行它时,似乎继续玩的循环都有效,但是每当 computerChoose 执行 randomGenerator 时,游戏结果都不会正确输出。请帮忙。我是java新手,我们只打算使用3种方法——指
我的石头剪刀布游戏遇到问题。当我玩游戏时,它会将胜利或失败记录为胜利和失败。我试图让它记录一场胜利和一场失败,并且不会为胜利和失败添加分数。有什么建议吗? import java.io.*; publ
我正在制作石头剪刀布游戏。 下面的代码可以工作,但是缺少两个关键的东西,我真的很想知道如何实现它: 如果出现平局,玩家必须能够重新选择。所以必须执行break语句,但是需要什么才能返回到while循环
显然是在学习java。我能够让游戏正常运行。但我需要这样做,以便如果用户放置 R/P/S 以外的东西,那么它默认为 Rock。我不需要循环。如果我放石头的话,游戏就完美了。如果我放置 RPS 以外的任
#RoShamBo import random count=0 while count -2: compnum=random.randint(0,2) usernum=int(inpu
我用Python做了一个石头、剪刀、布的游戏。如果用户输入"is",我如何让游戏重复进行?当用户输入除了石头、布或剪刀之外的任何内容时,我的代码似乎进入永无休止的循环 此外,我正在尝试了解何时何地应该
我还在和我叔叔一起学习 C,我在尝试编译我们一起编写的玩剪刀石头布的程序时遇到了另一个麻烦。我在该程序上还有更多工作要做,但我什至无法让大部分内容正常工作。 我使用 Make 命令从编译器收到的错误
这是我的石头剪刀布游戏。 http://jsfiddle.net/Renay/hL3j5hm6/6/ 如何添加动画,在给出结果之前,图像上下弹跳时有 3,2,1 倒计时。我尝试过添加各种功
我的类(class)本周将举行机器人竞赛,我们应该让我们的机器人战胜其他人的机器人。比赛项目为石头、布、剪刀、炸药、水气球。炸药胜过一切,除了水气球,而水气球只胜过炸药。老师写了竞技场,还有其他一些机
我正在使用 Python 创建一个非常简单的石头剪刀布游戏,但无法解决这个问题。每次我在命令提示符中输入答案时,它都会输出消息“无效输入,请重试!”,这就是我告诉它在存在无效输入时执行的操作。但是,我
我遇到了一个问题,选择根本没有更新。我已经列出了每次选择后应显示最后结果和当前分数的位置。分数运行良好,但选择根本没有更新。谢谢。 Dynamic Web Squirtle,
我的图片和消息正在更改为显示获胜、失败、平局,但我的 javascript 增量似乎不起作用,因为我的分数没有改变。请帮忙:) Rock, Paper, Scissors Rock
我正在用 C# 制作石头、剪刀、布游戏,目前在有人输入非 R、S 或 P 的输入时尝试显示消息时遇到问题。例如,我正在尝试获取默认值在 switch 语句中工作,但我没有运气。这就是我目前所拥有的。如
我希望代码获取用户在输入字段中输入的值并将其传递给变量 userChoice。我不知道为什么这段代码不起作用,唯一的学习方法就是问你们。 HTML: Choose your destiny !
我是一名优秀的程序员,十分优秀!