我的做法有什么问题..而逻辑,并继续逻辑
What is wrong with my do...while logic, and continue logic?
我是stackoverflow的新手,对编程也有些陌生,所以请不要介意我对代码格式的糟糕。我的代码有两个问题。
- 我的继续语句(如果玩家键入"y"或"Y",我用它来继续循环(不起作用。它仅在正确猜测后终止程序,这导致我:
2.My 继续计数器会超过0而不停止,我只是在程序的逻辑中看不到我的错误。
我看不出我的逻辑有问题。
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <ctime>
#include <random>
using namespace std;
int getNumber(); //random number prototype
double getScore(); //gets score
int chances = 7; //chances to guess with
int main()
{
int guess = 0,
random;
char retry = 'y'; //initialize retry to 'y'
cout << "This is a random number guessing game. " << "You will be guessing between 1-100."
<< "You have 7 chances. Good luck! n n" << endl;
random = getNumber(); //give the function a variable
do
{
cout << random << "n" << "n";
chances--;
cout << "Enter your guess: ";
cin >> guess;
if (guess == random)
{
cout << "You have won the game! " << "Your score was: " << getScore();
cout << "Would you like to retry? (Y or N): ";
cin >> retry;
if (retry == 'y' || retry == 'Y')
{
chances = 7;
guess = 0;
getNumber();
continue; //player can retry the game
}
else if (chances == 0)
{
cout << "You have no chances left. Retry? (Y or N): ";
cin >> retry;
if (retry == 'y' || retry == 'Y')
{
chances = 7;
guess = 0;
getNumber();
continue;
}
}
return 0;
}
else if (guess != random)
cout << "You got it wrong. n" << "You have: " << chances << " chances left" << endl << endl;
else
cout << "Incorrect Input. Please type a number." << endl << endl;
} while (guess != random);
return 0;
}
int getNumber()
{
unsigned seed = time(0); //seed the random number
srand(seed);
int randNum = rand() % 10 + 1; //random number in the range of 1-10
return randNum;
}
if (retry == 'y' || 'Y')
这是不正确的逻辑,这就是为什么您的代码无法按照您想要的方式工作的原因。您希望它是:
if (retry == 'y' || retry == 'Y')
在其他 if-else 语句中也修复此逻辑错误。
你会想看看这个
您的continue
语句跳到末尾并检查条件 guess != random
,其计算结果为 false 并退出do while
。您需要做的是将猜测重置为0等值,以便条件的计算结果为true
。
相关文章:
- 警告处理为错误这里有什么问题
- 最小硬币更换问题(自上而下方法)
- 为"adjacent"变量赋值时出现问题
- 我的神经网络不起作用 [XOR 问题]
- 在Ubuntu 16.04上安装Cilk时出现问题
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 编译包含字符串的代码时遇到问题
- Project Euler问题4的错误解决方案
- 循环后如何继续阅读
- 问题:什么是QAbstractItemView::NoEditTriggers的反面
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 静态数据成员的问题-修复链接错误会导致编译器错误
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 一个关于在C++中重载布尔运算符的问题
- 首要问题的答案让值班员搞错了
- setlocale的C++土耳其字符串问题
- 我的做法有什么问题..而逻辑,并继续逻辑
- USB继电器DLL问题-继续
- 没有正确使用互斥锁?继续过去的问题