Javascript用户输入,用于不输出整数的运算符

时间:2018-04-20 02:49:00

标签: javascript

我应该拿一些数字并在列表提示中给出它们。

编辑,已解决,谢谢

1 个答案:

答案 0 :(得分:1)

将变量与运算符连接起来就像使用"Num 1= " + num1一样,并且您可以直接在document.write中进行数学计算,而无需每次重新分配result

var entry;
var average;
var total = 0;

entry = prompt("enter first number");
entry = parseInt(entry);
var num1 = entry;
total = total + num1;

entry = prompt("enter second number");
entry = parseInt(entry);
var num2 = entry;
total = total + num2;

average = parseInt(total / 2);
document.write("<h2>Arithmetic and operators</h2>")
document.write('Num 1 = ' + num1 + ' <br> Num 2 = ' + num2 + ' <br> Average num = ' + average + '<br>')

document.write(num1 + ' + ' + num2 + ' = ' + ( num1 + num2 ));
document.write("<br>")

document.write(num1 + ' - ' + num2 + ' = ' + ( num1 - num2 ));
document.write("<br>")

document.write(num1 + ' / ' + num2 + ' = ' + ( num1 / num2 ));
document.write("<br>")

document.write(num1 + ' % ' + num2 + ' = ' + ( num1 % num2 ));
document.write("<br>")

document.write(num1 + ' * ' + num2 + ' = ' + ( num1 * num2 ));
document.write("<br>")