如果声明?如果数字为负数,请勿将数字添加到总变量中

时间:2013-04-17 19:09:21

标签: javascript if-statement negative-number

问题,当用户输入的数字为负数时,如何将其添加到将输出的总变量中?

以下代码!

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);
}

1 个答案:

答案 0 :(得分:1)

在提示用户输入一个号码后,只需用if语句检查它是否为否定:

if(userNumber >=0)
{ //do your stuff here}