如何添加两个数字? [英] How to add two numbers?

查看:71
本文介绍了如何添加两个数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

任何人都可以请帮我?当我试图减去这两个数字时也会发生这种情况。为什么这不起作用?

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


推荐答案

试试这个:

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

这篇关于如何添加两个数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆