這個問題在這里已經有了答案: 如何從提示框中獲取數值?[重復] (6個答案) 3 小時前關閉。
do {
var y = prompt("Enter a positive integer from 1 to 26");
var int = Number.isInteger(y);
var x = parseInt(y);
window.alert(int);
} while (x > 26 || x < 1 || int == false)
這是我撰寫的代碼,但每當我在瀏覽器上輸入整數時,它仍然會一直提醒 int 為 false。
uj5u.com熱心網友回復:
Number.isInteger() 用于判斷已經是數字的值是否為整數。
IE
Number.isInteger(5) == true
Number.isInteger(5.1) == false
將字串傳遞給Number.isInteger()將始終回傳 false。
uj5u.com熱心網友回復:
因為prompt()總是回傳一個字串。將其設為整數取決于您:
let y = prompt("Enter a positive integer from 1 to 26");
y = parseInt(y);
if (y === NaN) {
alert('Please enter a number');
} else {
while(x > 26 || x < 1 || int == false)
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/408528.html
標籤:
