Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/407.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript代码不断重复语句。我怎样才能阻止这一切?_Javascript - Fatal编程技术网

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

JavaScript代码不断重复语句。我怎样才能阻止这一切?,javascript,Javascript,我正在创建一个为期12天的圣诞节javascript程序,当我打印出该语句时,它会不断重复该语句。你能给我一些关于如何解决这个问题并使程序正常工作的建议吗 var day = ["first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth"]; var song = ""; for (var x = 0; x <= 1

我正在创建一个为期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);}
var-day=[“第一”、“第二”、“第三”、“第四”、“第五”、“第六”,
“第七”、“第八”、“第九”、“第十”、“第十一”、“第十二”];
var song=“”;

for(var x=0;xbreak语句在切换情况下丢失

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
大小写放在开关本身上,不需要单独的if语句。

switch语句要求在大小写中有一个break,并且在循环开始时,song变量需要设置为空,开关也需要设置为cases需要从零开始,以便每次都能得到正确的大小写:

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);
}
for(变量x=0;x<12;x++){
宋=“”;
歌曲+=“在”+日[x]+“圣诞节”;
歌曲+=“我的真爱给了我:”;
如果(x==0){
歌曲+=“梨树上的鹧鸪。”
} 
否则{
开关(x){
案例11:
歌曲+=(“十二鼓手鼓声”);
打破
案例10:
歌曲+=(“十一管风笛”);
打破
案例9:
宋+=(“十大领主阿乐平”);
打破
案例8:
歌曲+=(“九位女士跳舞”);
打破
案例7:
歌曲+=(“八个女佣挤奶”);
打破
案例6:
歌曲+=(“七只天鹅a-游泳”);
打破
案例5:
宋+=(“六只鹅产蛋”);
打破
案例4:
宋+=(“五枚金戒指”);
打破
案例3:
歌+=(“四只鸣叫的鸟”);
打破
案例2:
宋+=(“三只法国母鸡”);
打破
案例1:
宋+=(“两只斑鸠”);
打破
案例0:
歌+=(“和梨树上的鹧鸪”);
打破
违约:
}
}
console.log(歌曲);
}
var-day=[“第一”、“第二”、“第三”、“第四”、“第五”、“第六”,
“第七”、“第八”、“第九”、“第十”、“第十一”、“第十二”];
var dayMessages=[“梨树上的一只鹧鸪”,“梨树上的一只鹧鸪”,“两只斑鸠”,“三只法国母鸡”,“四只鸣叫的鸟”,“五枚金戒指”,“六只下蛋的鹅,”,
“七只天鹅a游泳”,“八个女佣a挤奶”,“十个领主a乐平”,“十个领主a乐平”,“十一个风笛手吹笛”,“十二个鼓手鼓乐,”;
var song=“”;

对于(var x=0;x),您可以为您的开关大小写字符串创建和等效的数组,并删除开关大小写,然后从该数组中使用
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);
}