为什么执行循环时为什么不能正确显示除数?

时间:2019-01-17 00:13:18

标签: javascript algorithm debugging

在输出中,说明输出中每个整数是'odd'还是'even'。

如果该数字可以被三整除,请说明该数字不是奇数还是偶数 该数字可被三除。

如果数字可以被二和三整除,而不是说数字是奇数, 甚至被三除;指出数字是“可以被二和三整除的”。

以下是我在尝试中看到的内容以及控制台中的输出。为什么某些条目被复制?在这里我不理解我的逻辑出了什么问题。

for(var i = 1; i <= 100; i++) {
    if(i % 2 === 0) {
    console.log("The number " + i + " is even" );
  } else {
    console.log("The number " + i + " is odd");
  }

    if(i % 3 === 0) {
    console.log("The number " + i + " is divisible by three");
  }

    if(i % 2 === 0 && i % 3 === 0) {
    console.log("The number " + i + " is divisible by two and three");
  }

}

`

(index):37 The number 1 is odd
(index):35 The number 2 is even
(index):37 The number 3 is odd
(index):41 The number 3 is divisible by three
(index):35 The number 4 is even
(index):37 The number 5 is odd
(index):35 The number 6 is even
(index):41 The number 6 is divisible by three
(index):45 The number 6 is divisible by two and three
(index):37 The number 7 is odd
(index):35 The number 8 is even
(index):37 The number 9 is odd
(index):41 The number 9 is divisible by three
(index):35 The number 10 is even
(index):37 The number 11 is odd
(index):35 The number 12 is even
(index):41 The number 12 is divisible by three
(index):45 The number 12 is divisible by two and three
(index):37 The number 13 is odd
(index):35 The number 14 is even
(index):37 The number 15 is odd
(index):41 The number 15 is divisible by three
(index):35 The number 16 is even
(index):37 The number 17 is odd
(index):35 The number 18 is even
(index):41 The number 18 is divisible by three
(index):45 The number 18 is divisible by two and three
(index):37 The number 19 is odd
(index):35 The number 20 is even
(index):37 The number 21 is odd
(index):41 The number 21 is divisible by three
(index):35 The number 22 is even
(index):37 The number 23 is odd
(index):35 The number 24 is even
(index):41 The number 24 is divisible by three
(index):45 The number 24 is divisible by two and three
(index):37 The number 25 is odd
(index):35 The number 26 is even
(index):37 The number 27 is odd
(index):41 The number 27 is divisible by three
(index):35 The number 28 is even
(index):37 The number 29 is odd
(index):35 The number 30 is even
(index):41 The number 30 is divisible by three
(index):45 The number 30 is divisible by two and three
(index):37 The number 31 is odd
(index):35 The number 32 is even
(index):37 The number 33 is odd
(index):41 The number 33 is divisible by three
(index):35 The number 34 is even
(index):37 The number 35 is odd
(index):35 The number 36 is even
(index):41 The number 36 is divisible by three
(index):45 The number 36 is divisible by two and three
(index):37 The number 37 is odd
(index):35 The number 38 is even
(index):37 The number 39 is odd
(index):41 The number 39 is divisible by three
(index):35 The number 40 is even
(index):37 The number 41 is odd
(index):35 The number 42 is even
(index):41 The number 42 is divisible by three
(index):45 The number 42 is divisible by two and three
(index):37 The number 43 is odd
(index):35 The number 44 is even
(index):37 The number 45 is odd
(index):41 The number 45 is divisible by three
(index):35 The number 46 is even
(index):37 The number 47 is odd
(index):35 The number 48 is even
(index):41 The number 48 is divisible by three
(index):45 The number 48 is divisible by two and three
(index):37 The number 49 is odd
(index):35 The number 50 is even
(index):37 The number 51 is odd
(index):41 The number 51 is divisible by three
(index):35 The number 52 is even
(index):37 The number 53 is odd
(index):35 The number 54 is even
(index):41 The number 54 is divisible by three
(index):45 The number 54 is divisible by two and three
(index):37 The number 55 is odd
(index):35 The number 56 is even
(index):37 The number 57 is odd
(index):41 The number 57 is divisible by three
(index):35 The number 58 is even
(index):37 The number 59 is odd
(index):35 The number 60 is even
(index):41 The number 60 is divisible by three
(index):45 The number 60 is divisible by two and three
(index):37 The number 61 is odd
(index):35 The number 62 is even
(index):37 The number 63 is odd
(index):41 The number 63 is divisible by three
(index):35 The number 64 is even
(index):37 The number 65 is odd
(index):35 The number 66 is even
(index):41 The number 66 is divisible by three
(index):45 The number 66 is divisible by two and three
(index):37 The number 67 is odd
(index):35 The number 68 is even
(index):37 The number 69 is odd
(index):41 The number 69 is divisible by three
(index):35 The number 70 is even
(index):37 The number 71 is odd
(index):35 The number 72 is even
(index):41 The number 72 is divisible by three
(index):45 The number 72 is divisible by two and three
(index):37 The number 73 is odd
(index):35 The number 74 is even
(index):37 The number 75 is odd
(index):41 The number 75 is divisible by three
(index):35 The number 76 is even
(index):37 The number 77 is odd
(index):35 The number 78 is even
(index):41 The number 78 is divisible by three
(index):45 The number 78 is divisible by two and three
(index):37 The number 79 is odd
(index):35 The number 80 is even
(index):37 The number 81 is odd
(index):41 The number 81 is divisible by three
(index):35 The number 82 is even
(index):37 The number 83 is odd
(index):35 The number 84 is even
(index):41 The number 84 is divisible by three
(index):45 The number 84 is divisible by two and three
(index):37 The number 85 is odd
(index):35 The number 86 is even
(index):37 The number 87 is odd
(index):41 The number 87 is divisible by three
(index):35 The number 88 is even
(index):37 The number 89 is odd
(index):35 The number 90 is even
(index):41 The number 90 is divisible by three
(index):45 The number 90 is divisible by two and three
(index):37 The number 91 is odd
(index):35 The number 92 is even
(index):37 The number 93 is odd
(index):41 The number 93 is divisible by three
(index):35 The number 94 is even
(index):37 The number 95 is odd
(index):35 The number 96 is even
(index):41 The number 96 is divisible by three
(index):45 The number 96 is divisible by two and three
(index):37 The number 97 is odd
(index):35 The number 98 is even
(index):37 The number 99 is odd
(index):41 The number 99 is divisible by three
(index):35 The number 100 is even

正确输出示例:

The number '1' is odd.
The number '2' is even.
The number '3' is divisible by three.
...
The number '6' is divisible by two and three.

4 个答案:

答案 0 :(得分:0)

您对何时打印输出的计算是独立的,即。他们一个接一个地执行。因此,您将获得多行输出。

为了实现“替代”要求,您需要重新排序并链接它们:

for (var i = 1; i <= 100; i++) {
    if (i % 2 === 0 && i % 3 === 0) {
        console.log("The number " + i + " is divisible by two and three");
    } else if (i % 3 === 0) {
        console.log("The number " + i + " is divisible by three");
    } else if (i % 2 === 0) {
        console.log("The number " + i + " is even" );
    } else {
        console.log("The number " + i + " is odd");
    }
}

答案 1 :(得分:0)

满足多个if条件,因此您得到了重复。尝试将if-else稍微移动一下:-

if (i % 2 === 0 && i % 3 === 0) {
    console.log("The number " + i + " is divisible by two and three");
} else if (i % 3 === 0) {
    console.log("The number " + i + " is divisible by three");
} else if (i % 2 === 0) {
    console.log("The number " + i + " is even");
} else {
    console.log("The number " + i + " is odd");
}

答案 2 :(得分:0)

之所以会发生这种情况,是因为如果一个值可以被6整除,那么它也可以被偶数3整除。要修复该值,您应该先检查6并仅在else分支中检查2和3

for(var i = 1; i <= 100; i++) {
  if(i % 2 === 0 && i % 3 === 0) {
    console.log("The number " + i + " is divisible by two and three");
  }
  else {
      if(i % 2 === 0) {
        console.log("The number " + i + " is even" );
      } else {
        if(i % 3 === 0) {
          console.log("The number " + i + " is divisible by three");
        }
        else {
          console.log("The number " + i + " is odd");
        }
      }
  }
} 

答案 3 :(得分:0)

条件语句的顺序很重要。 下面的代码应该可以提供您期望的结果。

for(var i = 1; i <= 100; i++) {
    if(i % 2 === 0 && i % 3 === 0) {
       console.log("The number " + i + " is divisible by two and three");
    }   
    else if(i % 3 === 0) {
        console.log("The number " + i + " is divisible by three");
    }
    else if(i % 2 === 0) {
        console.log("The number " + i + " is even" );
    } 
   else {
        console.log("The number " + i + " is odd");
    }
}