[英]JavaScript code keeps repeating statements. How can I stop this?
我正在创建一个为期 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;
}
在您的 switch 语句中,您错过了break
语句。 您也可以将x==0
案例放在 switch 本身上,不需要单独的 if 语句。
您的 switch 语句需要在 case 内中断,并且 Song 变量需要在循环开始时设置为空,而且您的 switch case 需要从零开始,以便每次都能获得正确的 case:
for (var x = 0; x < 12; x++) {
song = "";
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 11:
song += ("twelve drummers drumming, ");
break;
case 10:
song += ("eleven pipers piping, ");
break;
case 9:
song += ("ten lords a-leping, ");
break;
case 8:
song += ("nine ladies dancing, ");
break;
case 7:
song += ("eight maids a-milking, ");
break;
case 6:
song += ("seven swans a-swimming, ");
break;
case 5:
song += ("six geese a-laying, ");
break;
case 4:
song += ("five gold rings,");
break;
case 3:
song += ("four calling birds, ");
break;
case 2:
song += ("three french hens, ");
break;
case 1:
song += ("two turtle doves ");
break;
case 0:
song += ("and a partridge in a pear tree.");
break;
default:
}
}
console.log(song);
}
var day = ["first", "second", "third", "fourth", "fifth", "sixth",
"seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth"];
var dayMessages = ["a partridge in a pear tree.", "and a partridge in a pear tree.", "two turtle doves ", "three french hens, ", "four calling birds, ", "five gold rings,", "six geese a-laying, ",
"seven swans a-swimming, ", "eight maids a-milking, ", "ten lords a-leping, ", "ten lords a-leping, ", "eleven pipers piping, ", "twelve drummers drumming, "];
var song = "";
for (var x = 0; x <= 13; x++) {
song = "On the " + day[x] + " day of Christmas";
song += " my true love gave to me: ";
song += dayMessages[x];
console.log(song);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.