gpt4 book ai didi

c++ - 需要非常简单的 C++ for 循环的帮助

转载 作者:太空狗 更新时间:2023-10-29 23:50:30 24 4
gpt4 key购买 nike

for( int i = 0; i <= 10; i++ )
{
cout << setw(2) << i << setw(10) << ( i <= 5 ? cout <<" = Chipotle" : cout << " = McDonalds" ) << endl;
}

所以,我预计输出是:

 0 = Chipotle
1 = Chipotle
2 = Chipotle
3 = Chipotle
4 = Chipotle
5 = Chipotle
6 = McDonalds
7 = McDonalds
8 = McDonalds
9 = McDonalds
10 = McDonalds

(不用担心 setw 格式)

相反,我的 IDE 给了我:

 = Chipotle 0  0x602208                                                                                                             
= Chipotle 1 0x602208
= Chipotle 2 0x602208
= Chipotle 3 0x602208
= Chipotle 4 0x602208
= Chipotle 5 0x602208
= McDonalds 6 0x602208
= McDonalds 7 0x602208
= McDonalds 8 0x602208
= McDonalds 9 0x602208
= McDonalds10 0x602208

我哪里做错了?

最佳答案

您显示 cout 本身,删除三元运算符中的 cout:

cout << setw(2) << i << setw(10) << ( i <= 5 ? " = Chipotle" : " = McDonalds" ) << endl

关于c++ - 需要非常简单的 C++ for 循环的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29848781/

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