javascript 如何将两个数字相加?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16292523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 04:05:33  来源:igfitidea点击:

How to add two numbers?

javascript

提问by Pramito Rahman

I wrote a JavaScript calculator... but suppose when I give my first number as 2and the second number as 3, the result is showing 23, but I wanted to add the two numbers.

我写了一个 JavaScript 计算器……但是假设当我给我的第一个数字 as2和第二个数字 as 时3,结果显示为23,但我想将这两个数字相加。

Can anyone please help me? It is also happening when I try to minus the two numbers. Why isn't this working?

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

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

采纳答案by Satpal

Try this:

试试这个:

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

回答by Danny Beckett

The +sign is used to concatenate strings together, not to add them together mathematically.

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

You need to wrap your variables in parseInt(), e.g.

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

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

回答by zerkms

Convert a string to a number after you accept it from user:

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

a = parseInt(a, 10);

回答by Michael Robinson

Prompts return strings, you need to parse them as integers (you could also do floats using parseFloat)

提示返回字符串,您需要将它们解析为整数(您也可以使用浮点数parseFloat

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

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

回答by Vijendra Singh

The Promptmethod returns the entered value as a string.

Prompt方法将输入的值作为字符串返回。

So after Promptuse parseInt(), this function parses a string and returns an integer.

所以在Prompt使用之后parseInt(),这个函数解析一个字符串并返回一个整数。

回答by PleaseStand

The binary+operatorhas two uses: addition and string concatenation. Although you want the former, the latter is happening because window.prompt()returns a string.

二进制+运算符有两个用途:加法和字符串连接。虽然你想要前者,但后者正在发生,因为window.prompt()返回一个字符串。

To avoid this, you should do one of the following (read the documentation so you can understand the differences):

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

It is wise to check whether the numbers could be parsed (using isNaN(num), or possibly num === num) before trying to perform calculations with them, so your script can show a helpful error message instead of just carrying NaNthrough to its output.

在尝试使用它们执行计算之前检查数字是否可以解析(使用isNaN(num)或可能num === num)是明智的,这样您的脚本可以显示有用的错误消息,而不仅仅是执行NaN到其输出。

回答by Saiyam

Use:

利用:

var add = parseFloat(a)+parseFloat(b);

回答by zeeshan

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>"  )

I Hope this will help you.

我希望这能帮到您。

回答by zeeshan

You are adding to Strings; known as concatenation, you need to convert the String representations to numbersthen add them.

您正在添加字符串;称为串联,您需要将字符串表示形式转换为数字,然后将它们相加。

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

回答by Tamil Selvan C

Use

利用

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