问题,当用户输入的数字为负数时,如何将其添加到将输出的总变量中?
以下代码!
function lab10logicInLoopsPart1()
{
lCounter = 1;
var total = 0;
userNumber = 0;
while(lCounter < 6) {
lCounter++;
userNumber = prompt("Enter a number.");
total += +userNumber;
document.write("Entered number was: " + userNumber + "\n");
}
document.write("\nTotal: " + total);
}
答案 0 :(得分:1)
在提示用户输入一个号码后,只需用if语句检查它是否为否定:
if(userNumber >=0)
{ //do your stuff here}