【问题标题】:JavaScript code keeps repeating statements. How can I stop this?JavaScript 代码不断重复语句。我怎样才能阻止这个?
【发布时间】:2019-01-14 06:17:09
【问题描述】:

我正在创建一个为期 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 字符串创建和等效数组,并删除 switch case,然后从该数组中使用

标签: javascript


【解决方案1】:

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;
    }

【讨论】:

    【解决方案2】:

    在您的 switch 语句中,您错过了 break 语句。你也可以将x==0 case 放在 switch 本身上,不需要单独的 if 语句。

    【讨论】:

      【解决方案3】:

      您的 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);
      }
      

      【讨论】:

        【解决方案4】:
        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);
        }
        

        【讨论】:

          猜你喜欢
          • 2017-08-15
          • 2012-09-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-07-15
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多