gpt4 book ai didi

c++ - 如何在 C++ 中跳过 >=

转载 作者:太空宇宙 更新时间:2023-11-04 16:01:52 27 4
gpt4 key购买 nike

如果我的用户输入满足前一个 if 状态,我不知道如何让我的代码转到下一个 else if 语句。

#include <iostream>

using namespace std;

int main()
{
double input;
cout << "Time Calculator\n Enter the number of Seconds: " << endl;
cin >> input;
if (input < 60)
{
cout << "The time is " << input << " seconds." << endl;
}
else if (input >= 60)
{
cout << "The time is " << input << " minutes." << endl;
}
else if (input >= 3600)
{
cout << "The time is " << input << " hours." << endl;
}
else if (input >= 86400)
{
cout << "The time is " << input << " days." << endl;
}
return 0;
}

最佳答案

在你的代码中如果输入 > 60 它将满足条件并且不会执行其他部分所以首先检查是否输入 > 86400 如果不是然后检查输入是否 > 36000 如果不是然后检查输入 > 60

试试下面的代码,如果条件相反

#include <iostream>

using namespace std;

int main()
{
double input;
cout << "Time Calculator\n Enter the number of Seconds: " << endl;
cin >> input;
if (input < 60)
{
cout << "The time is " << input << " seconds." << endl;
}
else if (input >= 86400)
{
cout << "The time is " << input << " days." << endl;
}
else if (input >= 3600)
{
cout << "The time is " << input << " hours." << endl;
}
else if (input >= 60)
{
cout << "The time is " << input << " minutes." << endl;
}
return 0;
}

关于c++ - 如何在 C++ 中跳过 >=,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42383085/

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