我是新来的StackOverflow,也有些新的程序,所以请不要介意我的代码格式不佳。我的代码有两个问题。我的do ...逻辑和继续逻辑有什么问题?
- 我继续声明,如果玩家输入'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;
}
我不介意你可怜的格式。新的并不妨碍你花费一些时间和精力来很好地设计你的代码。举例来说,您可以查看任何现有的成功问题。 Stack Overflow有一个很棒的实时文章预览,所以你可以花费尽可能多的时间来创建好文章。 –
'如果(重试==“Y” ||“Y”)'这并不做什么,你认为它 – JackVanier