我必须为我的计算机科学课程编写一个简单的程序,我的老师要我用循环和开关打印出十二天的圣诞歌曲,我试图运行这段代码但是开关被跳过了完全。如果你可以帮助我,那将是伟大的,谢谢!
#include <iostream>
using namespace std;
int main()
{
for (int i = 12; i > 0; i--)
{
cout << "On the " << i << " day of Christmas my true love gave to me, \n";
switch (i)
{
case 1: "Twelve drummers drumming\n";
case 2: "Eleven pipers piping\n";
case 3: "Ten lords a-leaping\n";
case 4: "Nine ladies dancingn";
case 5: "Eight maids a-milking\n";
case 6: "Seven swans a-swimming\n";
case 7: "Six geese a-laying\n";
case 8: "Five gold rings\n";
case 9: "Four colly birds\n";
case 10: "Three french hens\n";
case 11: "Two turtle doves, and\n";
case 12: "A partridge in a pear tree\n";
break;
}
}
system("pause");
return 0;
}
答案 0 :(得分:1)
我正在尝试运行此代码但是交换机完全被跳过?不会跳过switch
,评估switch
表达式,但由于没有cout
语句,因此不会打印任何内容。
在每种情况下将cout
语句添加为
std::cout <<"A partridge in a pear tree"<<std::endl;
break;/* if required */
如果需要,请在每种情况下放置break
。
switch (i)
{
case 1: std::cout<<"Twelve drummers drumming"<<std::endl;
break; /* if required */
/* add remaining case */
}