X 变量在最后一次尝试简单猜谜游戏时发生变化,C++
X variable changes on the last try of simple guessing game, C++
我对编码和C++有点陌生。我试图制作一个猜谜游戏,将以前的猜测存储在一个数组中,该数组将在每次尝试失败后显示。
问题是,无论我输入什么,第三次尝试将始终注册为正确,并且x变量(即随机数(将更改为我输入的任何内容作为y[2]。在我将数组添加到 y 变量之前,代码运行良好,并调整了代码的其余部分以适应它。
我还对变量进行了调试。非常感谢任何和所有的帮助。谢谢!;)
//Generating Random Number:
#include <cstdlib> // or <stdlib.h>
#include <ctime> // or <time.h>
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
int x;
int y[2];
int z = 3;
int i;
bool done = false;
srand(time(NULL));
x = rand() % 10 + 1;
i = 0;
int s;
s = x;
while (done == false)
{
cout << "Guess a number between 1 to 10.n";
switch (i)
{
case 0: break;
case 1:
cout << "Previous guesses:n" << y[0] << endl << endl; break;
case 2:
cout << "Previous guesses:n" << y[0] << endl << y[1] << endl << endl; break;
}
cin >> y[i];
cout << "y[" << i << "] = " << y[i] << endl;
cout << "s = " << s << endl;
if (y[i] > s)
{
z--;
i++;
cout << "nLower!n" << "Tries remaining: " << z << endl << endl;
if (z == 0)
{
cout << "nSorry! You ran out of tries. :(";
Sleep (2000);
done = true;
}
}
else if (y[i] < s)
{
z--;
i++;
cout << "nHigher!n" << "Tries remaining: " << z << endl << endl;
if (z == 0)
{
cout << "nSorry! You ran out of tries. :(";
Sleep (2000);
done = true;
}
}
else if (y[i] == s)
{
cout << "i = " << i << endl;
cout << "nCongratulations!n" << "Your number is " << y[i] << ".n" << "Tries remaining: " << z;
done = true;
}
}
return(0);
}
int y[2]
表示您创建一个大小为 2 的数组(可以容纳两个元素(。
更重要的是,vector
或std::array
比内置数组更受欢迎
相关文章:
- C++取消引用指针.为什么会发生变化
- 为什么在读取文件大小时文件IO速度会发生变化
- 在C++游戏中与库存系统作斗争
- 换位表导致测试失败(但在游戏中运行良好)
- 为什么cudaMemGetInfo报告设备内存总量的变化
- Craps游戏问题,忽略if语句
- 矢量下标超出SFML游戏中的范围
- 我是 c++ 的新手.学习基本知识后,我想做井字游戏.对于印刷板,我在下面写代码,但它显示错误
- 如何避免LED在循环状态变化中闪烁?
- 当 I2C 值在C++中发生变化时收到通知
- 我在贪吃蛇游戏中收到了错误代码 -1073741571
- Gtkmm 会不断检查 Gtk::日历上的所选日期是否发生变化
- 我应该在简单的策略游戏中为各个派系使用类吗 - C++
- 游戏体验和升级
- 我的代码运行良好,但在游戏循环中中断
- 如何使用 SFML 在贪吃蛇游戏中定义游戏结束?
- 当我调用对象的方法时,对象的成员会发生变化
- 我可以使用任何好的逻辑来阻止计算机将 O 放在井字游戏中的现有 X 上
- X 变量在最后一次尝试简单猜谜游戏时发生变化,C++
- 在游戏更新循环中计算布尔的真/假变化