c++ - 为什么 if 语句没有打印出字符串?cpp
问题描述
我想在字符串中打印偶数和奇数,而不是在二进制中打印 true false | 1 / 0; 为什么输出仍然打印布尔值?
int main() {
printf("enter the two numbers\n");
int a;
int b;
cin >> a >> b;
string arr[9] = {"one","two","three","four","five","six","seven","eight","nine"};
for (int i = a; i <= b; i++)
{
if(i <=9){
cout << arr[i-1] << "\n";
}else{
cout << ( i%2 == 0)? "even" : "odd"; // print out 1/0 but did not print out the even and odd
cout << "\n";
}
}
return 0;
}
输出:
enter the two numbers
9 16
nine
1
0
1
0
1
0
1
解决方案
您缺少包含表达式的括号。三元的优先级低于<<
。
cout << (( i%2 == 0)? "even" : "odd");
推荐阅读
- javascript - 在 react 如何使用可重用组件作为 React Component 传递到 react-router-dom 组件道具?
- azure - 错误 ASPCONFIG:无法加载文件或程序集'CrystalDecisions.CrystalReports.Engine,版本 = 13.0.2000.0 Azure DevOps CI Pipline
- python - 如何在 python 中 __scrap__ 第二个标签与 BeautifulSoup 的链接
- c# - 在 Crystal Report 上将背景图像固定为 100%
- node.js - 如何检查 Angular 8 中的会话
- javascript - 向填充有数据表的现有表添加额外的列
- soap - 为什么要同时使用 XSD 和 WSDL 来定义域模型和 Web 服务契约
- javascript - 长字符串中的正则表达式搜索和替换
- c++ - 如何在 c++ win 32 中使用线程
- .net - 尝试在更新中返回 NotFound() 时出现 DBConcurrencyException