C ++歌曲打印问题

时间:2016-08-17 04:22:10

标签: c++ switch-statement

我正在练习我的编程技巧,他设计的程序可以打印圣诞节的十二天"歌曲,但用户输入1-12的数字,帮助确定打印数量。我接近我的代码,但它不是我想要的。目前代码如下所示:

include <iostream>
#include <string>
using namespace std;
int main()
{
    int day;
    cout << "How many days ( 1 to 12)?\n";
    cin >> day;
    if (day == 0) {

        return 0;
    }
    else if (day > 0) {   

    cout << "On the ";
        if (day == 2) {
        cout<< "2nd day of Christmas my true love gave to me\n";    
        }

        else if (day == 3) {
            cout << "3rd day of Christmas my true love gave to me\n"; 
        }
        else {
            cout << day << "th day of Christmas my true love gave to me\n"; 

        }
    switch (day) {
    case 12: cout << "Twelve Drummers Drumming\n";
    case 11: cout << "Eleven Pipers Piping\n";
    case 10: cout << "Ten Lords a-Leaping\n";
    case 9: cout << "Nine Ladies Dancing\n";
    case 8: cout << "Eight Maids a-Milking\n";
    case 7: cout << "Seven Swans a-Swimming\n";
    case 6: cout << "Six Geese a-Laying\n";
    case 5: cout << "Five Gold Rings\n";
    case 4: cout << "Four Calling Birds\n";
    case 3: cout << "Three French Hens\n";
    case 2: cout << "Two Turtle Doves, and\n";
    case 1: cout << "A Partridge in a Pear Tree\n\n";
    }
    }
    return 0;
}

因此用户输入一个数字,例如4,输出为:

How many days ( 1 to 12)?
4
On the 4th day of Christmas my true love gave to me
Four Calling Birds
Three French Hens
Two Turtle Doves, and
A Partridge in a Pear Tree

所以它就是这样,但不是我需要的方式,所需的输出是:

How many days (1 to 12)? 
4
On the 1st day of Christmas my true love gave to me
A partridge in a pear tree.

On the 2nd day of Christmas my true love gave to me
Two turtle doves, and
A partridge in a pear tree.

On the 3rd day of Christmas my true love gave to me
Three French hens,
Two turtle doves, and
A partridge in a pear tree.

On the 4th day of Christmas my true love gave to me
Four calling birds,
Three French hens,
Two turtle doves, and
A partridge in a pear tree.

我想我需要引入另一个开关,但实际上并不确定。

2 个答案:

答案 0 :(得分:1)

你需要循环遍历序列,参见下面的片段:

for (int i=1; i <= day; i++) {
    switch (i) {
        case 12: cout << "Twelve Drummers Drumming\n";
        case 11: cout << "Eleven Pipers Piping\n";
        case 10: cout << "Ten Lords a-Leaping\n";
        case 9: cout << "Nine Ladies Dancing\n";
        case 8: cout << "Eight Maids a-Milking\n";
        case 7: cout << "Seven Swans a-Swimming\n";
        case 6: cout << "Six Geese a-Laying\n";
        case 5: cout << "Five Gold Rings\n";
        case 4: cout << "Four Calling Birds\n";
        case 3: cout << "Three French Hens\n";
        case 2: cout << "Two Turtle Doves, and\n";
        case 1: cout << "A Partridge in a Pear Tree\n\n";
    }
}

我最初的订单倒退了,但现在我记得这些歌曲以A Partridge in a Pear Tree开头并从那里开始向上工作。

注意:循环仅针对1或更高的日期号执行。但是,您可能还需要包含一个检查来处理用户输入高于12的数字的情况。

答案 1 :(得分:0)

我会按以下方式进行:

#include <string>
#include <iostream>

using namespace std;

int main(int argc, char* argv[])
{
    string present[] = { "A Partridge in a Pear Tree",
                         "Two Turtle Doves, and",
                         "Three French Hens",
                         "Four Calling Birds",
                         "Five Gold Rings",
                         "Six Geese a-Laying",
                         "Seven Swans a-Swimming",
                         "Eight Maids a-Milking",
                         "Nine Ladies Dancing",
                         "Ten Lords a-Leaping",
                         "Eleven Pipers Piping",
                         "Twelve Drummers Drumming"
                        };

    int day;
    cout << "How many days ( 1 to 12)?\n";
    cin >> day;
    if (day == 0) {
        return 0;
    }
    else if (day > 0) {   

        for(int n = 1; n <= day; n++)
        {
            cout << "On the " << n;
            switch(n)
            {
                 case 1: cout << "st "; break;
                 case 2: cout << "nd "; break;
                 case 3: cout << "rd "; break;
                 default: cout << "th "; break;
            }
            cout << "day of Christmas my true love gave to me" << endl;

            for(int i = n - 1; i >= 0; i--)
            {
                cout << present[i] << endl;
            }
            cout << endl << endl;
        }
    }
    return 0;
}