我有这个赋值,允许用户输入有效的输入并使用do while循环显示结果。现在它可以允许用户输入,但不会显示计数器var的结果。任何帮助都会非常感谢
alert("Sum the Numbers Application");
var sum = 0;
var num = parseInt(prompt("Enter the top numbers to sum", "5"));
for (var i = 1; i <= num; i++) {
sum += i;
}
if (num <= 0 || num > 100 || isNaN(num)) {
alert("Not a valid entry.");
} else {
alert("Sum of the numbers from 1 through " + num + " is " + sum + ".")
}
do {
var num = parseInt(prompt("Enter the top numbers to sum", "5"));
}
while (num <= 0 || num > 100 || isNaN(num))
&#13;
答案 0 :(得分:0)
我认为这是理想的。 while循环需要在顶部检查有效响应。输入有效响应后,循环可以退出并计算总和。
alert("Sum the Numbers Application");
var sum = 0;
var valid = false;
do {
var num = parseInt(prompt("Enter the top numbers to sum", "5"));
if (num <= 0 || num > 100 || isNaN(num)) {
alert("Not a valid entry.");
} else {
valid = true;
}
}
while (!valid)
for (var i = 1; i <= num; i++) {
sum += i;
}
alert("Sum of the numbers from 1 through " + num + " is " + sum + ".")
&#13;