Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么算法在第一个条件之后结束?_C++_Algorithm_Loops - Fatal编程技术网

C++ 为什么算法在第一个条件之后结束?

C++ 为什么算法在第一个条件之后结束?,c++,algorithm,loops,C++,Algorithm,Loops,我正在设计一个用于游戏的数字猜测算法 有人能提出为什么算法在第一个条件之后结束吗 #include <iostream> using namespace std; int main() { int num = 5; int guess; cout << "Guess a number \n"; cin >> guess; if (guess==num) { cout << "You guessed the correct num

我正在设计一个用于游戏的数字猜测算法

有人能提出为什么算法在第一个条件之后结束吗

#include <iostream>

using namespace std;

int main()
{

int num = 5;
int guess;

cout << "Guess a number \n";
cin >> guess;


if (guess==num)
{
    cout << "You guessed the correct number \n";
}
else if (guess < num)
{
    cout << "Your guess is lower than the number \n";
    cout << "Guess again \n";
    cin >> guess;
}
else
{
    cout << "Your guess is higher than the number \n";
    cout << "Guess again \n";
    cin >> guess;
}


return 0;
#包括
使用名称空间std;
int main()
{
int num=5;
智力猜测;
猜不透;
如果(猜测==num)
{

如果你想重复这个算法,你需要某种循环

比如说

#include <iostream>

using namespace std;

int main()
{
    int num = 5;
    int guess;

    cout << "Guess a number \n";

    do
    {
        cin >> guess;

        if ( guess == num )
        {
            cout << "You guessed the correct number \n";
        }
        else if ( guess < num )
        {
            cout << "Your guess is lower than the number \n";
            cout << "Guess again \n";
        }
        else
        {
            cout << "Your guess is higher than the number \n";
            cout << "Guess again \n";
        }

    } while ( guess != num );

    return 0;
}
#包括
使用名称空间std;
int main()
{
int num=5;
智力猜测;
猜不透;
如果(猜测==num)
{

如果你想重复这个算法,你需要某种循环

比如说

#include <iostream>

using namespace std;

int main()
{
    int num = 5;
    int guess;

    cout << "Guess a number \n";

    do
    {
        cin >> guess;

        if ( guess == num )
        {
            cout << "You guessed the correct number \n";
        }
        else if ( guess < num )
        {
            cout << "Your guess is lower than the number \n";
            cout << "Guess again \n";
        }
        else
        {
            cout << "Your guess is higher than the number \n";
            cout << "Guess again \n";
        }

    } while ( guess != num );

    return 0;
}
#包括
使用名称空间std;
int main()
{
int num=5;
智力猜测;
猜不透;
如果(猜测==num)
{

cout如果您想再猜一次,我建议您使用循环。否则,您的代码将按预期工作

while(number != guess)
        {
            if(number * 2 < guess){
                cout << "Way to high. Try again." << endl;
                cin >> guess;
            }

            if(number / 2 > guess)
            {
                cout << "Tip : My number is NOT low. Try again." << endl;
                cin >> guess;
            }

            if(number < guess)
            {
                cout << "To high try something lower. Feed me a number." << endl;
                cin >> guess;
            }
            if(number > guess)
                cout << "To low, try again." << endl;
                cin >> guess;
        }
while(数字!=猜测)
{
如果(数字*2<猜测){
猜不到;
}
如果(数字/2>猜测)
{
猜不到;
}
如果(数字<猜测)
{
猜不到;
}
如果(数字>猜测)
猜不到;
}

如果您想再次猜测,我建议您使用循环。否则,您的代码将按预期工作

while(number != guess)
        {
            if(number * 2 < guess){
                cout << "Way to high. Try again." << endl;
                cin >> guess;
            }

            if(number / 2 > guess)
            {
                cout << "Tip : My number is NOT low. Try again." << endl;
                cin >> guess;
            }

            if(number < guess)
            {
                cout << "To high try something lower. Feed me a number." << endl;
                cin >> guess;
            }
            if(number > guess)
                cout << "To low, try again." << endl;
                cin >> guess;
        }
while(数字!=猜测)
{
如果(数字*2<猜测){
猜不到;
}
如果(数字/2>猜测)
{
猜不到;
}
如果(数字<猜测)
{
猜不到;
}
如果(数字>猜测)
猜不到;
}

@user3528438不知道怎么做,但我相信我会在网上找到一些东西。谢谢您的时间:D@user3528438不知道怎么做,但我相信我会在网上找到一些东西谢谢你的时间:D@AgelosNuho完全没有。@AgelosNuho完全没有。:)