gpt4 book ai didi

C++:打印列表

转载 作者:行者123 更新时间:2023-11-28 01:26:27 24 4
gpt4 key购买 nike

下面的代码包含我创建的以相反顺序从 1 到 10 编号的表格。 (这是没有中断的代码,后来改变了。)

#include <iostream>

using namespace std;

int main()
{
switch(1) {
case 1: cout << "1. blue" << endl;
case 2: cout << "2. orange.." << endl;
case 3: cout << "3. yellow.." << endl;
case 4: cout << "4. green.." << endl;
case 5: cout << "5. purple.." << endl;
case 6: cout << "6. red.." << endl;
case 7: cout << "7. teal.." << endl;
case 8: cout << "8. aqua.." << endl;
case 9: cout << "9. white.." << endl;
case 10: cout << "10. gray.." << endl;


}

}

我试图让它每次循环运行时,程序将输出对应于下一个较低数字的消息,一个一个地输出。(带中断的代码)

#include <iostream>

using namespace std;

int main() {
for(int i = 10; i>0; --i){
switch(i) {
case 1: cout << "1. blue" << endl;
break;
case 2: cout << "2. orange.." << endl;
break;
case 3: cout << "3. yellow.." << endl;
break;
case 4: cout << "4. green.." << endl;
break;
case 5: cout << "5. purple.." << endl;
break;
case 6: cout << "6. red.." << endl;
break;
case 7: cout << "7. teal.." << endl;
break;
case 8: cout << "8. aqua.." << endl;
break;
case 9: cout << "9. white.." << endl;
break;
case 10: cout << "10. gray.." << endl;
break;
}
}
}

我在向下循环列表并一一列出每个数字/颜色时遇到问题。

在列出每个项目的最后,我希望程序询问用户是否要停止或重新启动循环。

最佳答案

您需要创建一个变量来保存将传递到您的 switch 语句中的变量。你的 switch 语句需要从 10 开始往下走,因为你想先打印 case 10。

#include <iostream>

using namespace std;

int main() {
for(int i = 10; i>0; --i){
switch(i) {
case 1: cout << "1. blue" << endl;
break;
case 2: cout << "2. orange.." << endl;
break;
case 3: cout << "3. yellow.." << endl;
break;
case 4: cout << "4. green.." << endl;
break;
case 5: cout << "5. purple.." << endl;
break;
case 6: cout << "6. red.." << endl;
break;
case 7: cout << "7. teal.." << endl;
break;
case 8: cout << "8. aqua.." << endl;
break;
case 9: cout << "9. white.." << endl;
break;
case 10: cout << "10. gray.." << endl;
break;
}
}
}

关于C++:打印列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53562887/

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