简体   繁体   English

Javascript计算器添加无法正常工作

[英]Javascript calculator addition is not working properly

Here is my really basic Javascript calculator code. 这是我真正基本的Javascript计算器代码。 Except for the addition symbol, my calculator is just working fine. 除了添加符号,我的计算器工作正常。 However, addition is not doing its job: instead it combines them. 然而,添加并没有完成它的工作:相反,它结合了它们。 How to fix it? 怎么解决?

var first = prompt("First number");
var second = prompt("Second number");
parseInt(first);
parseInt(second);
var islem = prompt("Is it +/-/* or /?");
if (islem == "+" ) {
    document.write(first + second);
}
else if ( islem == "-") {
    document.write(first - second);
}
else if ( islem == "*" ) {
    document.write(first * second);
}
else {
    document.write(first / second);
}

The parseInt() function parses a string argument and returns an integer. parseInt()函数解析字符串参数并返回一个整数。

You have to assign parsed value to your variable, like this: 您必须为您的变量分配parsed值,如下所示:

first=parseInt(first);
second=parseInt(second);

Or simply, 或简单地说,

document.write(parseInt(first)+parseInt(second));

See reference here . 这里的参考。

 var first = prompt("First number"); var second = prompt("Second number"); parseInt(first); parseInt(second); var islem = prompt("Is it +/-/* or /?"); if (islem == "+" ) { document.write(parseInt(first)+parseInt(second)); } else if ( islem == "-") { document.write(first-second); } else if ( islem == "*" ) { document.write(first*second); } else { document.write(first/second); } 

When you use parseInt() , you need to capture the returned value so that you can use that number going forward. 当您使用parseInt() ,您需要捕获返回的值,以便您可以使用该数字。 Right now, you are calling parseInt() and not doing anything with the returned value, so it is lost immediately. 现在,您正在调用parseInt()并且没有对返回的值执行任何操作,因此它会立即丢失。

It is also advisable to use the optional second parameter to parseInt() , which is the radix value that sets the base of the number you are working with. 建议使用parseInt()的可选第二个参数,它是设置您正在使用的数字基数的基数值。 Typically, you'll want that to be 10 when working with base 10 numbers. 通常情况下,你会想,要成为10与基地10个号码中工作时。 This is really important because if the string you are working with starts with a 0 and you don't supply the radix, the operation will assume you are working with an octal value and if the string were to start with 0x , then the operation will assume you are working with a hex (base 16) value. 这非常重要,因为如果您正在使用的字符串以0开头并且您不提供基数,则操作将假定您正在使用八进制值,如果字符串以0x ,则操作将假设您正在使用十六进制(基数为16)的值。

See more about using parseInt() . 了解有关使用parseInt()更多信息。

The code should be: 代码应该是:

var first = prompt("First number");
var second = prompt("Second number");
first = parseInt(first, 10);
second = parseInt(second, 10);

You are not storing the parseInt result anywhere, and instead performing operations on strings. 您没有将parseInt结果存储在任何位置,而是对字符串执行操作。 Since + is also a string concatenation operation, you get the concatenated string as a result. 由于+也是字符串连接操作,因此您将获得连接字符串。 Try changing lines 3-4 to: 尝试将3-4行更改为:

first = parseInt(first, 10); //don't forget the base!
second = parseInt(second, 10);
//TBD: add some error handling code to check values

Here is the right program: 这是正确的计划:

var first = prompt("First number");
var second = prompt("Second number");
var firstParsedInteger = parseInt(first);
var secondParsedInteger = parseInt(second);
var islem = prompt("Is it +/-/* or /?");
if (islem == "+" ) {
    document.write(firstParsedInteger + secondParsedInteger);
}
else if ( islem == "-") {
    document.write(first-second);
}
else if ( islem == "*" ) {
    document.write(first*second);
}
else {
    document.write(first/second);
}

Note: You were never using the parsed value. 注意:您从未使用过解析的值。 In javascript, if you are using var for numbers and using '+' operator. 在javascript中,如果您使用var作为数字并使用“+”运算符。 this treats those numbers as string not as a number. 这会将这些数字视为字符串而不是数字。 Remember to parse integer when playing with var and + in javascript. 记得在javascript中使用var和+时解析整数。

These answers are all great! 这些答案都很棒! I'd go one step further and 我会更进一步

1) Call parseInt() on the prompts immediately, so that you're not declaring as many unnecessary variables 1)立即在提示符上调用parseInt() ,这样就不会声明多少不必要的变量

2) Use a switch statement instead of an if/else! 2)使用switch语句而不是if / else! It'll make it easier to add other cases, for exponentiation, etc. This one's more of a style choice, though :) 它会更容易添加其他情况,取幂等等。这更像是一种风格选择,但:)

3) You can also consider using parseFloat() instead of parseInt() so that it can handle decimal inputs! 3)您还可以考虑使用parseFloat()而不是parseInt()以便它可以处理十进制输入!

var first = parseInt(prompt("First number"));
var second = parseInt(prompt("Second number"));
var islem = prompt("Is it +/-/* or /?");
switch (islem) {
    case "+":
        document.write(first+second);
        break;
    case "-":
        document.write(first-second);
        break;
    case "*":
        document.write(first*second);
        break;
    case "/":
        document.write(first/second);
        break;
    default:
        document.write("Excuse me? Give me an operator, please!");
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM