0

好吧,它是一个简单的项目,它完成了所有应该做的事情,但它不会在它应该退出的时候退出。

我尝试了许多不同的方法,包括 goto 语句,我尝试了一个 if 循环,但除了错误什么也没有。当前的代码没有给出任何错误,只是我不知道去哪里让它退出。不必花哨,这只是我的第二个程序

// C// Guess My Number
// The classic number guessing game

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;
int main()
{

srand(static_cast<unsigned int>(time(0)));  //seed random number generator

int secretNumberE = rand() % 10 + 1; 
int secretNumberM = rand() % 100 + 1;// random number between 1 and 100
int secretNumberH = rand() % 1000 + 1;
int tries = 0;
int guess;
char play;


{ 
cout << "\tWelcome to Guess My Number\n\n";
START:
cout << "Difficulty Levels\n\n";
cout << "1 - Easy\n";
cout << "2 - Normal\n";
cout << "3 - Hard\n\n";


int choice;
cout << "Choice: ";
cin >> choice;

switch (choice)
{
case 1:
cout << "You picked Easy.\n";
do
{
    cout << "Enter a guess 1-10: ";
    cin >> guess;
    ++tries;

    if (guess > secretNumberE)
    {
        cout << "Too high!\n\n";
    }
    else if (guess < secretNumberE)
    {
        cout << "Too low!\n\n";
    }
    else
    {
        cout << "\nThat's it! You got it in " << tries << " guesses!\n";
    }

} while (guess != secretNumberE);
std::cout << "Do you want to play again y/n? ";
cin >> play;
if ( play = 'y' ){
goto START;
}
else if (play = 'n') 
{
cout << " Thank you for playing. ";
return 0;
}
break;
case 2:
cout << "You picked Normal.\n";
do
{
    cout << "Enter a guess 1-100: ";
    cin >> guess;
    ++tries;

    if (guess > secretNumberM)
    {
        cout << "Too high!\n\n";
    }
    else if (guess < secretNumberM)
    {
        cout << "Too low!\n\n";
    }
    else
    {
        cout << "\nThat's it! You got it in " << tries << " guesses!\n";
    }

} while (guess != secretNumberM);
std::cout << "Do you want to play again y/n? ";
cin >> play;
if ( play = 'y' ){
goto START;
}
else if (play = 'n') 
{
cout << " Thank you for playing. ";
return 0;
}
break;
case 3:
cout << "You picked Hard.\n";
do
{
    cout << "Enter a guess 1-10: ";
    cin >> guess;
    ++tries;

    if (guess > secretNumberH)
    {
        cout << "Too high!\n\n";
    }
    else if (guess < secretNumberH)
    {
        cout << "Too low!\n\n";
    }
    else
    {
        cout << "\nThat's it! You got it in " << tries << " guesses!\n";
    }

} while (guess != secretNumberH);
std::cout << "Do you want to play again y/n? ";
cin >> play;
if ( play = 'y' ){
goto START;
}
else if (play = 'n') 
{
cout << " Thank you for playing. ";
return 0;
}
break;

default:
cout << "You made an illegal choice.\n";
goto START;
return 0;
}}}
4

3 回答 3

2
if ( play = 'y' ){
goto START;
}
else if (play = 'n') 
{
cout << " Thank you for playing. ";
return 0;
}

第一个if总是正确的,因为您使用的是赋值运算符而不是相等运算符。您需要用于==比较两个值。

于 2013-09-11T23:43:21.960 回答
1
...

char play;


{ 

...

您的代码中似乎有一个额外{的东西用于包装您的东西。

对我来说很清楚你需要处理的事情,缩进。您的 IDE 中可能有一个自动格式化快捷方式。开始使用它。不可能很好地阅读您的代码,从长远来看,这将导致一系列错误,因为您会遇到一些简单的事情。

goto除非您知道自己在做什么,否则强烈建议不要使用它。

开始学习功能/方法,它们将挽救您的生命并全面改善您的工作流程。

你可以goto用它代替。

函数的例子..

int startGame(){
   cout << "Difficulty Levels\n\n";
   cout << "1 - Easy\n";
   cout << "2 - Normal\n";
   cout << "3 - Hard\n\n";


   int choice = 0;
   cout << "Choice: ";
   cin >> choice;
   return choice;
}

其他事情。

if ( play = 'y' )需要if ( play == 'y' )冲洗重复此操作,因为您正在尝试检查是否相等(条件)而不是设置一个值=

此外,ALWAYS INITIALIZE VALUESint choice;应该是int choice = 0;或一些默认值。

于 2013-09-11T23:52:49.907 回答
0

使用“break”键

do
{
    if(mycondition)
    {
        break;
    }
} while (loopCondition);
于 2013-09-11T23:43:32.017 回答