本文介绍了JavaScript计算器5 + 5 = 55?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嘿,我做了一个JavaScript计算器,想尝试5 + 5,它给了我55,然后是10?如果我尝试5/5怎么会给我1罚款如果我尝试5-5会给我0罚款,而且与我不理解的时间相同我的代码问题也想尝试console.log它不会工作,所以我使用document.write
Hey I made a javascript calculator and want the to try 5 + 5 it give me 55 other then 10 ? How ever if I try 5 / 5 it give me 1 fine if i try 5 - 5 it gives me 0 fine and same with times i don't under stand the problem with my code also want to try console.log it doesn't work so I use document.write
这是我的代码
var Plus = function()
{
var N1 = prompt("Enter first number");
var N2 = prompt("Enter second number");
var sum = (N1 + N2);
document.write(sum);
}
var Minus = function()
{
var N1 = prompt("Enter first number");
var N2 = prompt("Enter second number");
var sum = (N1 - N2);
document.write(sum);
}
var Division = function()
{
var N1 = prompt("Enter first number");
var N2 = prompt("Enter second number");
var sum = (N1 / N2);
document.write(sum);
}
var Times = function()
{
var N1 = prompt("Enter first number");
var N2 = prompt("Enter second number");
var sum = (N1 * N2);
document.write(sum);
}
var Answer = prompt("Plus ?, Minus ?, Division ? or Times ?");
if( Answer === "Plus")
{
Plus();
}
else
{
if( Answer === "Minus")
{
Minus();
}
else
{
if( Answer === "Division")
{
Division();
}
else
{
if( Answer === "Times")
{
Times();
}
else
{
document.write("How did you get here?");
}
}
}
}
推荐答案
N1
和 N2
是字符串,而不是数字。将两个字符串加在一起将它们串联在一起,这就是您的错误。
N1
and N2
are strings, not numbers. Adding two strings together concatenates them, which is what your "error" is.
您需要将它们解析为数字:
You need to parse them into numbers:
var N1 = Number(prompt("Enter first number"));
这篇关于JavaScript计算器5 + 5 = 55?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!