gpt4 book ai didi

JavaScript 代码不断重复语句。我怎样才能阻止这个?

转载 作者:行者123 更新时间:2023-12-02 08:03:36 25 4
gpt4 key购买 nike

我正在创建一个 12 天的圣诞节 javascript 程序,当我打印出该语句时,它不断重复该语句。你能给我一些关于如何解决这个问题并让程序正常工作的建议吗?

var day = ["first", "second", "third", "fourth", "fifth", "sixth", 
"seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth"];
var song = "";

for (var x = 0; x <= 13; x++) {
song += "On the " + day[x] + " day of Christmas";
song += " my true love gave to me: ";

if (x == 0) {
song += "a partridge in a pear tree."
}
else {
switch (x) {
case 12:
song += ("twelve drummers drumming, ");
case 11:
song += ("eleven pipers piping, ");
case 10:
song += ("ten lords a-leping, ");
case 9:
song += ("nine ladies dancing, ");
case 8:
song += ("eight maids a-milking, ");
case 7:
song += ("seven swans a-swimming, ");
case 6:
song += ("six geese a-laying, ");
case 5:
song += ("five gold rings,");
case 4:
song += ("four calling birds, ");
case 3:
song += ("three french hens, ");
case 2:
song += ("two turtle doves ");
case 1:
song += ("and a partridge in a pear tree.");
break;
}
}
console.log(song);}

最佳答案

switch case 中缺少 break 语句。

switch (x) {
case 12:
song += ("twelve drummers drumming, ");
break;
case 11:
song += ("eleven pipers piping, ");
break;
case 10:
song += ("ten lords a-leping, ");
break;
case 9:
song += ("nine ladies dancing, ");
break;
case 8:
song += ("eight maids a-milking, ");
break;
case 7:
song += ("seven swans a-swimming, ");
break;
case 6:
song += ("six geese a-laying, ");
break;
case 5:
song += ("five gold rings,");
break;
case 4:
song += ("four calling birds, ");
break;
case 3:
song += ("three french hens, ");
break;
case 2:
song += ("two turtle doves ");
break;
case 1:
song += ("and a partridge in a pear tree.");
break;
}

关于JavaScript 代码不断重复语句。我怎样才能阻止这个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54176548/

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