簡體   English   中英

JavaScript 代碼不斷重復語句。 我怎么能阻止這個?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM