gpt4 book ai didi

c++ - 为什么if语句没有打印出字符串? cpp

转载 作者:行者123 更新时间:2023-12-01 14:39:40 24 4
gpt4 key购买 nike

我想在字符串而不是二进制中打印偶数和奇数,以获取true false | 1/0;
为什么输出仍显示 bool(boolean) 值?

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");

关于c++ - 为什么if语句没有打印出字符串? cpp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60955636/

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