c++:从嵌套的do while循环到嵌套的while循环 我在C++中有这个猜谜游戏程序。 我在一个嵌套的do-while循环中完成了它。 如何在while循环中实现它而不是在do-while中实现它

c++:从嵌套的do while循环到嵌套的while循环 我在C++中有这个猜谜游戏程序。 我在一个嵌套的do-while循环中完成了它。 如何在while循环中实现它而不是在do-while中实现它,c++,nested,do-while,C++,Nested,Do While,在用户表示要再次播放后,您需要重新初始化num,否则guess将始终等于num。 在此之后,将猜测设置为num永远不会像-1那样的值 还要考虑一下do while与while的区别。做一次某事,然后检查一个条件。while循环首先检查一个条件,然后在条件满足时执行某些操作。这感觉像是一个任务或练习,所以我不打算写它,但您应该有足够的信息来解决这个问题:用下面的循环替换您的嵌套循环。只嵌套一个,而不是第一个Do while,正如您在上面的评论中所说的 guess=-1; // while(gu

在用户表示要再次播放后,您需要重新初始化num,否则guess将始终等于num。 在此之后,将猜测设置为num永远不会像-1那样的值


还要考虑一下do while与while的区别。做一次某事,然后检查一个条件。while循环首先检查一个条件,然后在条件满足时执行某些操作。这感觉像是一个任务或练习,所以我不打算写它,但您应该有足够的信息来解决这个问题:

用下面的循环替换您的嵌套循环。只嵌套一个,而不是第一个Do while,正如您在上面的评论中所说的

  guess=-1; //
while(guess!=num)
    {
     cin >> guess;

     if (guess > num)
     {
        cout << "Your guess is high . Guess again !"<<endl;
     }
     else if (guess < num)
     {
        cout << "Your guess is low . Guess again !"<<endl;
     }
     else
     {
         cout << "That is Correct, You win"<<endl;
     }
 }

您需要将用于猜测数字的代码放入函数中,例如guessTheNumber。一开始叫它一次。在它跳出循环后,它将从函数返回。在该函数调用之后,询问用户是否要重试。如果是,则再次调用该函数,例如猜测号码,直到用户说不。一旦用户说不,退出您的程序。这将把它减少到一个do/while循环

伪代码:

int main()
{
   guessTheNumber();
   Do you want to guess again?
   if choice is yes, call guessTheNumber();
   else
   return 0;
}

你一定是新加坡人。。不管怎样,你可以试试这个

cout << "Guess a number between 0 - 10 : "<<endl;
cin >> guess;

while(true)
{
        if (guess > num)
            cout << "Your guess is high . Guess again !"<<endl;
        else if (guess < num)    
            cout << "Your guess is low . Guess again !"<<endl;            
        else
        {
            cout << "That is Correct, You win"<<endl;
            cout <<"\nWould you like to give the game another try ? (Yes or no)"<<endl;
            cin>>choice;
            if (choice.compare("no")==0  //If user enter any other inputs, game continues
                 break;
        }
        cin >> guess;
} 
有几种方法可以实现这一点。在本例中,由于您已经检查了条件num==guess,因此可以在while循环中删除该检查。使用while循环仅用于循环


显式地将guess设置为不可能是num的数字,例如set guess=-1感觉有点像硬编码:-P

确保while语句之前存在逻辑默认值。除此之外,代码是你的

#include <iostream>
#include <string>
#include <ctime>
#include <stdlib.h>
using namespace std;


int main()
{
srand(time(0));
int guess;
string choice , name;

cout << "What is your name ?"<<endl;
cin>>name;
cout << "*******************************"<<endl;
cout << "Welcome to the Hi-Lo Game , "<<name<<endl;
cout << "*******************************"<<endl;

choice = "Yes";
while (choice=="Yes")
{
    int num = rand() % 10 + 0;
    cout << "Guess a number between 0 - 10 : "<<endl;
    guess = -1;
    while (guess !=num)
    {
        cin >> guess;

        if (guess > num)
        {
            cout << "Your guess is too high . Guess again !"<<endl;
        }
        else if (guess < num)
        {
            cout << "Your guess is too low . Guess again !"<<endl;
        }
        else
        {
            cout << "That is Correct, You win"<<endl;
        }
    }

    cout <<"\nWould you like to give the game another try ? (Yes or no)"<<endl;

    cin>>choice;

}


return 0;
}

我为你的游戏循环创建了一个函数,让一切变得更清晰。还要注意,srand正在寻找一个无符号int

#include <iostream>
#include <string>
#include <ctime>
using namespace std;

void rungame()
{
    int num = rand() % 10 + 0;
    int guess = -1;

    cout << "Guess a number between 0 - 10 : "<<endl;
    while (guess != num)
    {
        cin >> guess;
        if (guess > num)
        {
            cout << "Your guess is high . Guess again !"<<endl;
        }
        if (guess < num)
        {
            cout << "Your guess is low . Guess again !"<<endl;
        }
    }
}

int main()
{
    srand( (unsigned int) time(NULL) );
    string choice = "Yes";
    while (choice == "Yes")
    {
     rungame();
     cout << "That is Correct, You win"<<endl;
        cout <<"\nWould you like to give the game another try ? (Yes or no)"<<endl;
        cin>>choice;
    }
    return 0;
}

如果循环的迭代次数事先未知,但取决于指定条件的实现,则通常使用While循环。 while循环的基本形式:

while条件

{

}

块后面的语句

•在这些类型的循环条件中,在语句块开始时测试循环,直到满足条件为止。 •条件是一个逻辑表达式,其结果必须是逻辑数据类型bool。当结果变为逻辑false false 0时,跳过block命令循环,程序从循环语句块后的第一条语句继续。
•与在循环中一样,在开始测试条件时,循环中的语句块可能不会执行一次。

在添加include时对我有效。当我为数字输入一个非整数时,我得到一个循环。若要修复此问题,您需要检查输入中的错误。名称字段不能包含空白。可能需要阅读整行的名称。是否希望嵌套循环是while循环而不是do-while循环?是的,Buck,这就是我想要的。我试过了,但没能做对。虽然确实可行,但无限循环不是一个好主意。它们实际上是在添加工作,因为您不能重用值来自然触发WHILE中内置的退出条件!
    block of statements