为什么我的第一个 if 语句有效,但我的 else if 语句在它之后不起作用
Why does my first if statement work, but my else if statement after it doesn't work
为什么我的第一个if语句工作,但我的else if语句没有?只是希望有人能发现我遗漏的东西。当我输入800和1800之间的时间时,代码继续正常运行,但当我输入600时,它跳转到我以非法格式输入的else语句。
if ((time_start >= 800) && (time_start <= 1800))
{
cout << "How many minutes did your call last? ";
cin >> minutes;
cost = minutes * 0.40;
cout << setprecision(2) << fixed << cost;
system("pause");
keepgoing = false;
}
else if ((time_start < 800) && (time_start > 1800))
{
cout << "How many minutes did your call last? ";
cin >> minutes;
cost = minutes * 0.25;
cout << setprecision(2) << fixed << cost;
system("pause");
keepgoing = false;
}
你有一个'逻辑错误':在else if
中使用||
而不是&&
。
else if ((time_start < 800) && (time_start > 1800))
一个数字不可能既小于800又大于1800。我想你的意思是:
else if ((time_start < 800) || (time_start > 1800))
当我问这个问题时,我意识到我的第二个else if语句应该是or ||语句,而不是&&
change
else if ((time_start <800) (time_start> 1800
else if ((time_start <800) || (time_start> 1800
您可以将代码简化为这样,不需要使用else语句。将值存储在需要在
之前定义的系数变量中// else coef (default value)
float coef = 0.25;
if ((time_start >= 800) && (time_start <= 1800))
{
coef = 0.40; // if true
}
cout << "How many minutes did your call last? ";
cin >> minutes;
// use coef here and remove duplicate calls
cost = minutes * coef;
cout << setprecision(2) << fixed << cost;
system("pause");
keepgoing = false;
相关文章:
- 我的简单if-else语句是如何无法访问的代码
- Insert函数不适用于2 if语句C++
- If语句未被求值C++
- C++嵌套if语句,基本货币交换
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- 是否可以使用if constexpr删除控制流语句
- 我似乎对if/else的基本语句有问题:/
- If语句在c++中被忽略
- 比较if语句中的数组值和int值
- 以在Qt中的IF语句中设置时间延迟
- Craps游戏问题,忽略if语句
- c++:定义if语句中的模板
- C++嵌套的 If 语句中,它无法按预期工作
- 如果中的多个语句是否与多个 if 相同?
- C++ If/Else 语句被跳过
- 使用语句if时逻辑的问题
- 如何添加语句if-else
- 如何将语句if(isalpha(c))从c++转换为MIPS ?
- 在if语句if C++中创建对象时出现作用域问题
- if语句-if/else和参数放置C++