Javascript 如何添加两个数字?

Javascript 如何添加两个数字?,javascript,Javascript,我写了一个JavaScript计算器。。。但是假设当我给出第一个数字为2,第二个数字为3,结果显示23,但我想把这两个数字相加 有人能帮我吗?当我尝试减去这两个数字时,也会发生这种情况。为什么这不起作用 var cal = prompt("Please enter what type of calculation you want to do\n if you wanna add enter = 1\n if you want to minus enter = 2\n if you want t

我写了一个JavaScript计算器。。。但是假设当我给出第一个数字为
2
,第二个数字为
3
,结果显示
23
,但我想把这两个数字相加

有人能帮我吗?当我尝试减去这两个数字时,也会发生这种情况。为什么这不起作用

var cal = prompt("Please enter what type of calculation you want to do\n
if you wanna add enter = 1\n
if you want to minus enter = 2\n
if you want to divide enter = 3\n
if you want to multiply enter = 4");

if (cal == 1) {
    var a = prompt("Please enter your first number");
    var b = prompt("please enter your second number");

    alert("The result is , " + a+b);
}

if (cal == 2) {
    var c = prompt("Please enter your first number");
    var d = prompt("please enter your second number");

    alert("the result is , " + c - d);
}

您正在添加字符串;称为串联,然后需要添加它们

alert("The result is , " + (parseInt(a) + parseInt(b)));

+
符号用于将字符串连接在一起,而不是在数学上将它们相加

您需要将变量包装在
parseInt()
中,例如

alert("The result is , " + parseInt(a)+parseInt(b));

从用户处接受字符串后,将其转换为数字:

a = parseInt(a, 10);

提示返回字符串,您需要将其解析为整数(也可以使用
parseFloat
进行浮点运算)

alert(“结果是,”+(parseInt(a)+parseInt(b))

尝试以下操作:

var cal = prompt("Please enter what type of calculation you want to do\n" +
  "if you want to add enter = 1\n" +
  "if you want to minus enter = 2\n" +
  "if you want to divide enter = 3\n" +
  "if you want to multiply enter = 4");

if (cal == 1) {
    var a = prompt("Please enter your first number");
    var b = prompt("please enter your second number");

    alert("The result is , " + (Number(a) + Number(b)));
}

else if (cal == 2) {
    var c = prompt("Please enter your first number");
    var d = prompt("please enter your second number");

    alert("the result is , " + (Number(c) - Number(d)));
}
使用


我认为这是因为var是动态类型变量,所以您的变量被转换为字符串

请注意,函数可以接收双精度,因此
parseInt
可能无法正常工作

function Add(a,b)
{
   var result = null;

   if (isNaN(a) || isNaN(b))
   {
      alert("Please send the number");
      return;
   }

   a = Number(a);
   b= Number(b);

   return a+b;
}

Prompt
方法以字符串形式返回输入的值


因此,在
提示后
使用
parseInt()
,此函数解析字符串并返回整数。

二进制有两种用途:加法和字符串串联。虽然您想要前者,但后者正在发生,因为它返回一个字符串

为了避免这种情况,您应该执行以下操作之一(阅读文档以便理解差异):

  • 使用将字符串解析为整数
  • 使用将字符串解析为浮点数
  • 使用一元运算符将字符串强制转换为数字:
  • 或者,也可以使用
在尝试使用数字执行计算之前,最好先检查数字是否可以被解析(使用,或者可能使用
num===num
),这样脚本就可以显示有用的错误消息,而不只是将
NaN
传递到其输出中。

使用:

var add = parseFloat(a)+parseFloat(b);
num1=window.prompt(“请输入您的num1:”);
num2=窗口。提示(“请输入您的num2:”;
var sum=parseInt(num1)+parseInt(num2);
document.writeln(“两个整数,“+num1+”和“+num2+”之和为“+sum+”,”)

我希望这会对您有所帮助。

使用
parseInt()
将它们转换为整数值您可能还需要注意,cal==1和cal==2是有效的,因为当使用双等于时,它会尝试转换,而当使用三等于时,它也会匹配类型(无论它们是数字还是相同的值)
var add = parseFloat(a)+parseFloat(b);
num1 = window.prompt("Please Enter Your Num1 :");
num2 = window.prompt("Please Enter Your Num2 :");
var sum = parseInt(num1)+ parseInt(num2);
document.writeln("<h1> The summ of two integers,"+num1+", and,"+num2+", is ,"+sum+",</h1>"  )