圣诞节12天,没有循环

时间:2014-12-15 14:52:10

标签: c++

我是一个相当弱的程序员,并得到了以下。我已经设置了语句,我可以在完成时清理它,但是例如,当用户输入“6”时,例如它将遍历第1节到第6节。

#include<iostream>

using namespace std;

int main()
{
int input;

cout << "Please enter a number from 1-12, USE '999' to close." << endl;
cin >> input;

for (int input = 1; input <= 12; input++)
{

    if (input == 1)
    {
        cout << "\nOn the 1st day of Christmas my true love gave to me,\n A Partridge in a Pear Tree." << endl;
    }

    else if (input == 2)
    {
        cout << "\nOn the 2nd day of Christmas my true love gave to me :\nTwo Turtle Doves,\n And a Partridge in a Pear Tree." << endl;
    }
    else if (input == 3)
    {
        cout << "\n\nOn the 3rd day of Christmas my true love gave to me,";
        cout << "Three French Hens\nTwo Turtle Doves,\nAnd a Partridge in a Pear Tree." << endl;
    }
    else if (input == 4)
    {
        cout << "\n\nOn the fourth day of Christmas my true love sent to me :\n";
        cout << "four Calling Birds\n3 French Hens\n2 Turtle Doves\nand a Partridge in a Pear Tree" << endl;
    }
    else if (input == 5)
    {
        cout << "\n\nOn the fifth day of Christmas my true love sent to me:\n";
        cout << " FIVE GOLDEN RINGS!, Four Calling Birds, three French Hens, 2 turtle doves\n";
        cout << "And a partridge in a pear tree.";
    }
    else if (input == 6)
    {
        cout << "\n\nOn the sixth day of Christmas my true love sent to me :\n6 Geese a Laying\n5 Golden Rings\n4 Calling Birds\n3 French Hens\n2 Turtle Doves\nand a Partridge in a Pear Tree\n";
    }
    else if (input == 7)
    {
        cout << "\n\nOn the seventh day of Christmas my true love sent to me :\n7 swans are swimming,\n6 Geese a Laying\n5 Golden Rings\n4 Calling Birds\n3 French Hens\n2 Turtle Doves\nand a Partridge in a Pear Tree\n";
    }
    else if (input == 8)
    {
        cout << "\n\nOn the eigth day of Christmas my true love sent to me :\n8 maids are milking,\n7 swans are swimming,\n6 Geese a Laying\n5 Golden Rings\n4 Calling Birds\n3 French Hens\n2 Turtle Doves\nand a Partridge in a Pear Tree\n";

    }
    else if (input == 9)
    {
        cout << "\n\nOn the nineth day of Christmas my true love sent to me :\n9 ladies dancing,\n8 maids are milking,\n7 swans are swimming,\n6 Geese a Laying\n5 Golden Rings\n4 Calling Birds\n3 French Hens\n2 Turtle Doves \nand a Partridge in a Pear Tree\n";

    }
    else if (input == 10)
    {
        cout << "\n\nOn the tenth day of Christmas my true love sent to me :\n10 lords a leaping,\n9 ladies dancing,\n8 maids are milking,\n7 swans are swimming,\n6 Geese a Laying\n5 Golden Rings\n4 Calling Birds\n3 French Hens\n2 Turtle Doves \n and a Partridge in a Pear Tree\n";

    }
    else if (input == 11)
    {
        cout << "\n\nOn the eleventh day of Christmas my true love sent to me :\n11 Pipers piping,\n10 lords a leaping,\n9 ladies dancing,\n8 maids are milking,\n7 swans are swimming,\n6 Geese a Laying\n5 Golden Rings\n4 Calling Birds\n3 French Hens\n2 Turtle Doves \nand a Partridge in a Pear Tree\n";

    }
    else if (input == 12)
    {
        cout << "\n\nOn the twevle day of Christmas my true love sent to me :\n12 Drummers Drumming,\n11 Pipers piping,\n10 lords a leaping,\n9 ladies dancing,\n8 maids are milking,\n7 swans are swimming,\n6 Geese a Laying\n5 Golden Rings\n4 Calling Birds\n3 French Hens\n2 Turtle Doves\n and a Partridge in a Pear Tree\n";

    }
    else if (input == 999)
    {
        cout << "Thanks, Good bye!";
        return 0;
    }
    else
    {
        cout << "Terminating program";
        return 0;
    }
}
system("pause");
return 0;
}

谢谢你们

6 个答案:

答案 0 :(得分:2)

更改for循环中的变量。当用户对输入变量进行输入时,你输入的变量在for循环中发生了变化,所以只需使用其他变量

int main()
{
int input;

cout << "Please enter a number from 1-12, USE '999' to close." << endl;
cin >> input;

for (int x = 1; x <= 12; x++)
{
 // bla bla bla

答案 1 :(得分:2)

这看起来像我曾经做过的家庭作业。

您正在for循环中重置输入变量。

如果您想保持代码相同,可以使用这样的switch语句:

switch(input){
    case 1  :
       cout << "\nOn the 1st day of Christmas my true love gave to me,\n A Partridge in a Pear Tree." << endl;
       break;
    //the rest of your cases
}

如果你想使用for循环,你需要在循环中使用不同的变量。

答案 2 :(得分:2)

您的循环变量与您正在读取的循环变量具有相同的名称,因此您将隐藏您读取的值,循环始终从1到12运行。

您不需要(并且这是一个坏主意)为每个数字重现整个内容。
(本练习的主要内容是让您发现歌曲中的模式并利用该模式以避免重复。)

你可以通过&#34; break-less&#34;切换,如果你不想循环:

const char* numbers[] = {"first", "second", and so on....
cout << "On the " << numbers[input - 1] << " day of Christmas my true love gave to me:\n";
switch (input)
{
    case 12: cout << "Twelve drummers drumming,\n"; // No break; fall through to next case
    case 11: cout << "Eleven pipers piping,\n";

    // ...

    case 2: cout << "Two turtle doves, and\n";
    case 1: cout << "A partridge in a pear tree\n";
}

或表和循环,如果你想要一个循环:

const char* gifts[] = {"A partridge in a pear tree", ... and so on

const char* numbers[] = {"first", "second", and so on....
cout << "On the " << numbers[input - 1] << " day of Christmas my true love gave to me:\n";

for (int i = input - 1; i >= 0; i--)
{
     cout << gifts[i] << ",";
     if (i == 1)
         cout << " and";
     cout << "\n";
}

答案 3 :(得分:0)

刚刚对for (int input = 1; input <= 12; input++)for (int i = 1; i <= input; i++)的for语句进行了一些更正,并更改了if / else集团中input的{​​{1}}:

i

所以它会显示从1到输入的语句。

答案 4 :(得分:0)

也许你的意思是以下

#include <iostream>

using namespace std;

int main()
{
    int input;

    do
    {
        input = 999;
        cout << "Please enter a number from 1-12, USE '999' to close." << endl;

    } while ( cin >> input && input != 999 && ( input < 1 || input > 12 ) );

    if ( input == 999 )
    {
        cout << "Thanks, Good bye!";
        return 0;
    }


    for ( int i = 1; i <= input; i++ )
    {
        if ( i == 1 )
        {
            cout << "\nOn the 1st day of Christmas my true love gave to me,\n A Partridge in a Pear Tree." << endl;
        }

        else if ( i == 2 )
        {
            cout << "\nOn the 2nd day of Christmas my true love gave to me :\nTwo Turtle Doves,\n And a Partridge in a Pear Tree." << endl;
        }
        else if ( i == 3 )
        {
            cout << "\n\nOn the 3rd day of Christmas my true love gave to me,";
            cout << "Three French Hens\nTwo Turtle Doves,\nAnd a Partridge in a Pear Tree." << endl;
        }
        else if ( i == 4 )
        {
            cout << "\n\nOn the fourth day of Christmas my true love sent to me :\n";
            cout << "four Calling Birds\n3 French Hens\n2 Turtle Doves\nand a Partridge in a Pear Tree" << endl;
        }
        else if ( i == 5 )
        {
            cout << "\n\nOn the fifth day of Christmas my true love sent to me:\n";
            cout << " FIVE GOLDEN RINGS!, Four Calling Birds, three French Hens, 2 turtle doves\n";
            cout << "And a partridge in a pear tree.";
        }
        else if ( i == 6 )
        {
            cout << "\n\nOn the sixth day of Christmas my true love sent to me :\n6 Geese a Laying\n5 Golden Rings\n4 Calling Birds\n3 French Hens\n2 Turtle Doves\nand a Partridge in a Pear Tree\n";
        }
        else if ( i == 7 )
        {
            cout << "\n\nOn the seventh day of Christmas my true love sent to me :\n7 swans are swimming,\n6 Geese a Laying\n5 Golden Rings\n4 Calling Birds\n3 French Hens\n2 Turtle Doves\nand a Partridge in a Pear Tree\n";
        }
        else if ( i == 8 )
        {
            cout << "\n\nOn the eigth day of Christmas my true love sent to me :\n8 maids are milking,\n7 swans are swimming,\n6 Geese a Laying\n5 Golden Rings\n4 Calling Birds\n3 French Hens\n2 Turtle Doves\nand a Partridge in a Pear Tree\n";
        }
        else if ( i == 9 )
        {
            cout << "\n\nOn the nineth day of Christmas my true love sent to me :\n9 ladies dancing,\n8 maids are milking,\n7 swans are swimming,\n6 Geese a Laying\n5 Golden Rings\n4 Calling Birds\n3 French Hens\n2 Turtle Doves \nand a Partridge in a Pear Tree\n";
        }
        else if ( i == 10 )
        {
            cout << "\n\nOn the tenth day of Christmas my true love sent to me :\n10 lords a leaping,\n9 ladies dancing,\n8 maids are milking,\n7 swans are swimming,\n6 Geese a Laying\n5 Golden Rings\n4 Calling Birds\n3 French Hens\n2 Turtle Doves \n and a Partridge in a Pear Tree\n";
        }
        else if ( i == 11 )
        {
            cout << "\n\nOn the eleventh day of Christmas my true love sent to me :\n11 Pipers piping,\n10 lords a leaping,\n9 ladies dancing,\n8 maids are milking,\n7 swans are swimming,\n6 Geese a Laying\n5 Golden Rings\n4 Calling Birds\n3 French Hens\n2 Turtle Doves \nand a Partridge in a Pear Tree\n";
        }
        else if ( i == 12 )
        {
            cout << "\n\nOn the twevle day of Christmas my true love sent to me :\n12 Drummers Drumming,\n11 Pipers piping,\n10 lords a leaping,\n9 ladies dancing,\n8 maids are milking,\n7 swans are swimming,\n6 Geese a Laying\n5 Golden Rings\n4 Calling Birds\n3 French Hens\n2 Turtle Doves\n and a Partridge in a Pear Tree\n";

        }
    }

    cout << "Terminating program";

    return 0;
}

如果要输入12,则程序输出将为

Please enter a number from 1-12, USE '999' to close.

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 fourth day of Christmas my true love sent to me :
four Calling Birds
3 French Hens
2 Turtle Doves
and a Partridge in a Pear Tree


On the fifth day of Christmas my true love sent to me:
 FIVE GOLDEN RINGS!, Four Calling Birds, three French Hens, 2 turtle doves
And a partridge in a pear tree.

On the sixth day of Christmas my true love sent to me :
6 Geese a Laying
5 Golden Rings
4 Calling Birds
3 French Hens
2 Turtle Doves
and a Partridge in a Pear Tree


On the seventh day of Christmas my true love sent to me :
7 swans are swimming,
6 Geese a Laying
5 Golden Rings
4 Calling Birds
3 French Hens
2 Turtle Doves
and a Partridge in a Pear Tree


On the eigth day of Christmas my true love sent to me :
8 maids are milking,
7 swans are swimming,
6 Geese a Laying
5 Golden Rings
4 Calling Birds
3 French Hens
2 Turtle Doves
and a Partridge in a Pear Tree


On the nineth day of Christmas my true love sent to me :
9 ladies dancing,
8 maids are milking,
7 swans are swimming,
6 Geese a Laying
5 Golden Rings
4 Calling Birds
3 French Hens
2 Turtle Doves 
and a Partridge in a Pear Tree


On the tenth day of Christmas my true love sent to me :
10 lords a leaping,
9 ladies dancing,
8 maids are milking,
7 swans are swimming,
6 Geese a Laying
5 Golden Rings
4 Calling Birds
3 French Hens
2 Turtle Doves 
 and a Partridge in a Pear Tree


On the eleventh day of Christmas my true love sent to me :
11 Pipers piping,
10 lords a leaping,
9 ladies dancing,
8 maids are milking,
7 swans are swimming,
6 Geese a Laying
5 Golden Rings
4 Calling Birds
3 French Hens
2 Turtle Doves 
and a Partridge in a Pear Tree


On the twevle day of Christmas my true love sent to me :
12 Drummers Drumming,
11 Pipers piping,
10 lords a leaping,
9 ladies dancing,
8 maids are milking,
7 swans are swimming,
6 Geese a Laying
5 Golden Rings
4 Calling Birds
3 French Hens
2 Turtle Doves
 and a Partridge in a Pear Tree
Terminating program

答案 5 :(得分:-3)

那是因为你在for循环中创建了一个新变量,隐藏了之前创建的输入。
如果我理解正确,你可以使用像

这样的东西
for (; input <= 12; input++)
//more code