gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:20:00 25 4
gpt4 key购买 nike

我正在设计一种用作游戏的猜数算法。

谁能提出为什么算法在第一个条件后结束?

#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;

最佳答案

如果您希望重复该算法,则需要某种循环。

例如

#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;
}

关于c++ - 为什么算法在第一个条件后结束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35686517/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com