gpt4 book ai didi

c++ - 圣诞节 12 天,不循环

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

我是一个相当弱的程序员,并得到了以下信息。我设置了语句,我可以在完成后清理它,但是例如,当用户输入“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;
}

谢谢大家

最佳答案

改变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

关于c++ - 圣诞节 12 天,不循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27486697/

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