作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
在输出中,说明每个整数在输出中是“奇数”还是“偶数”。
如果这个数能被三整除,不是说这个数是奇数还是偶数,而是说该数字“可以被三整除”。
如果这个数既能被二又能被三整除,而不是说这个数是奇数,偶数或能被三整除;声明该数字“可以被二和三整除”。
以下是我在尝试中看到的内容以及控制台中的输出。为什么有些条目被复制了?不明白我这里的逻辑有什么问题。
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.
最佳答案
满足多个 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");
}
关于javascript - 为什么在执行我的循环时可分性不能正确显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54227279/
我是一名优秀的程序员,十分优秀!