将两个数字相加,得到 5+10 = 510 而不是 15
Adding two numbers together, get 5+10 = 510 instead of 15
我刚开始学习 JavaScript,我正在摆弄一些代码,但我似乎找不到真正将两个变量相加并计算它们总和的方法。我声明了 3 个变量,其中我将一个变量设置为答案,将另外 2 个变量设置为两个数字。所以基本上是 a、b、c,其中 c = (a + b)。但是,每当我尝试 运行 代码时,结果都会以 'ab' 而不是 'a + b' 结束,所以如果 a = 5 和 b = 10,它会说“510”而不是“15”。
所有其他符号,如“-”、“/”和“*”都按预期工作,唯一不工作的是“+”。
我认为计算机认为我正在尝试打印出这两个字符串,但我想将它们相加,就像您发出警报时所做的那样,例如:alert("Hello World" + a);
我的思考方向是正确的还是其他问题?这是源代码:
function addTwoNumbers(firstNumber,secondNumber,numberAdded){
if(numberAdded == '+'){
numberAdded = (firstNumber + secondNumber);
alert("The summ of the two numbers is equal to: " + numberAdded);
}
else if(numberAdded == '-'){
numberAdded = (firstNumber - secondNumber);
alert("The difference of the two numbers is equal to: " + numberAdded);
}
else if(numberAdded == '/')
{
numberAdded = (firstNumber / secondNumber);
alert("The 'kvot' of the two numbers is equal to: " + numberAdded);
}
else if(numberAdded == '*'){
numberAdded = (firstNumber * secondNumber);
alert("The product of the two numbers is equal to: " + numberAdded);
}
else
{
alert("I told you to use '+, -, / and *' not anything else!");
}
}
var checker = true;
while(checker == true){
alert("You will now be prompted to enter two numbers.");
var firstNumber = prompt("Please enter the first number.");
var secondNumber = prompt("Please enter the second number.");
var numberAdded = prompt("Would you like to use '+, -, /, or * ?'");
checker = false;
addTwoNumbers(firstNumber, secondNumber, numberAdded);
if(numberAdded != '+' && numberAdded != '-' && numberAdded != '/' && numberAdded != '*'){
checker = true;
}
option = 0;
while(option != 'y' && option != 'n'){
var option = prompt("Would you like to make a calculation again? (y/n)");
if(option == 'y'){
checker = true;
}
else if(option == 'n'){
checker = false;
}
else{
alert("I said (y/n), try again...");
}
}
}
prompt
函数return一个字符串。因此,当您在两个字符串上使用 +
运算符时,您会将它们组合在一起。相反,将数字字符串解析为数字:
var firstNumber = parseInt(prompt("Please enter the first number."), 10);
var secondNumber = parseInt(prompt("Please enter the second number."), 10);
我刚开始学习 JavaScript,我正在摆弄一些代码,但我似乎找不到真正将两个变量相加并计算它们总和的方法。我声明了 3 个变量,其中我将一个变量设置为答案,将另外 2 个变量设置为两个数字。所以基本上是 a、b、c,其中 c = (a + b)。但是,每当我尝试 运行 代码时,结果都会以 'ab' 而不是 'a + b' 结束,所以如果 a = 5 和 b = 10,它会说“510”而不是“15”。
所有其他符号,如“-”、“/”和“*”都按预期工作,唯一不工作的是“+”。
我认为计算机认为我正在尝试打印出这两个字符串,但我想将它们相加,就像您发出警报时所做的那样,例如:alert("Hello World" + a);
我的思考方向是正确的还是其他问题?这是源代码:
function addTwoNumbers(firstNumber,secondNumber,numberAdded){
if(numberAdded == '+'){
numberAdded = (firstNumber + secondNumber);
alert("The summ of the two numbers is equal to: " + numberAdded);
}
else if(numberAdded == '-'){
numberAdded = (firstNumber - secondNumber);
alert("The difference of the two numbers is equal to: " + numberAdded);
}
else if(numberAdded == '/')
{
numberAdded = (firstNumber / secondNumber);
alert("The 'kvot' of the two numbers is equal to: " + numberAdded);
}
else if(numberAdded == '*'){
numberAdded = (firstNumber * secondNumber);
alert("The product of the two numbers is equal to: " + numberAdded);
}
else
{
alert("I told you to use '+, -, / and *' not anything else!");
}
}
var checker = true;
while(checker == true){
alert("You will now be prompted to enter two numbers.");
var firstNumber = prompt("Please enter the first number.");
var secondNumber = prompt("Please enter the second number.");
var numberAdded = prompt("Would you like to use '+, -, /, or * ?'");
checker = false;
addTwoNumbers(firstNumber, secondNumber, numberAdded);
if(numberAdded != '+' && numberAdded != '-' && numberAdded != '/' && numberAdded != '*'){
checker = true;
}
option = 0;
while(option != 'y' && option != 'n'){
var option = prompt("Would you like to make a calculation again? (y/n)");
if(option == 'y'){
checker = true;
}
else if(option == 'n'){
checker = false;
}
else{
alert("I said (y/n), try again...");
}
}
}
prompt
函数return一个字符串。因此,当您在两个字符串上使用 +
运算符时,您会将它们组合在一起。相反,将数字字符串解析为数字:
var firstNumber = parseInt(prompt("Please enter the first number."), 10);
var secondNumber = parseInt(prompt("Please enter the second number."), 10);