我使用Javascript和HTML制作了二次方程求解器,但是当我点击"计算"按钮它只是得到" a"和" c"值然后乘以-1。
我是Javascript的初学者,所以我不太了解对象。
以下是代码:
var a, b, c, xone, xtwo;
function getValues() {
function getValues() {
if (document.getElementById('signone').value == "+") {
a = document.getElementById('vara').value;
} else {
a = document.getElementById('vara').value * (-1);
}
if (document.getElementById('signtwo').value == "+") {
b = document.getElementById('varb').value;
} else {
b = document.getElementById('varb').value * (-1);
}
if (document.getElementById('signthree').value == "+") {
c = document.getElementById('varc').value;
} else {
c = document.getElementById('varc').value * (-1);
}
}
}
function getSolution() {
xone = ((-1 * b) + Math.sqrt((b * b) - 4 * a * c)) / (2 * a);
xtwo = ((-1 * b) - Math.sqrt((b * b) - 4 * a * c)) / (2 * a);
}
function showSolution() {
document.getElementById('showone').innerHTML = "x1 = " + xone;
document.getElementById('showtwo').innerHTML = "x2 = " + xtwo;
}

<h1> Quadratic equation calculator </h1>
<p>This calculator is going to find the two values of <i>x</i> of the equation typed.</br>In order to use it properly, you have to fill all of the boxes</br>
and click <q>ok</q>
</p>
</br>
<form>
<select id="signone">
<option value="+">+</option>
<option value="-">-</option>
</select>
<input id="vara" type="text" name="firstvar" placeholder="type the coeficient a" />x2
<select id="signtwo">
<option value="+">+</option>
<option value="-">-</option>
</select>
<input id="varb" type="text" name="secondvar" placeholder="type the coeficient b " />x
<select id="signthree">
<option value="+">+</option>
<option value="-">-</option>
</select>
<input id="varc" type="text" name="thirdvar" placeholder="type the coeficient c" />=0
</form>
</br>
<button type="button" onclick="getValues();getSolution();showSolution();">Calculate</button>
<p id="showone">X1 =</p>
</br>
<p id="showtwo">X2 =</p>
&#13;
答案 0 :(得分:1)
当您从文字输入中获取值时,该值为字符串,并且不是 Javascript中的数字。这就是为什么每当对其进行操作时,都会导致NaN。例如,正在发生的事情是&#39; 3&#39; * -1导致NaN,因为&#39; 3&#39;不是一个数字。 3是一个数字,但是&#39; 3&#39; (作为字符串)不是。
有一个简单的解决方案。您可以使用parseInt()
将字符串值转换为整数。但是,如果输入十进制数,最好使用parseFloat
。例如,正确的代码如下所示:
a = parseFloat(document.getElementById('vara').value) * (-1)
在许多情况下,您也打破了DRP规则,并且不要重复自己&#34;,因为您的代码非常重复,并且可以通过迭代循环更轻松地分解。每当你有一堆&#34; if&#34;像你这样的陈述,它通常可以循环分解。我会给你这样做的挑战。
如下所述,(我错过了),你也有两次这个功能,这是一个问题。
答案 1 :(得分:0)
问题是getValues()
没有做任何事情。函数内部有一个额外的函数定义,你永远不会调用内部函数。只需定义一次。
var a, b, c, xone, xtwo;
function getValues() {
if (document.getElementById('signone').value == "+") {
a = document.getElementById('vara').value;
} else {
a = document.getElementById('vara').value * (-1);
}
if (document.getElementById('signtwo').value == "+") {
b = document.getElementById('varb').value;
} else {
b = document.getElementById('varb').value * (-1);
}
if (document.getElementById('signthree').value == "+") {
c = document.getElementById('varc').value;
} else {
c = document.getElementById('varc').value * (-1);
}
}
function getSolution() {
xone = ((-1 * b) + Math.sqrt((b * b) - 4 * a * c)) / (2 * a);
xtwo = ((-1 * b) - Math.sqrt((b * b) - 4 * a * c)) / (2 * a);
}
function showSolution() {
document.getElementById('showone').innerHTML = "x1 = " + xone;
document.getElementById('showtwo').innerHTML = "x2 = " + xtwo;
}
<h1> Quadratic equation calculator </h1>
<p>This calculator is going to find the two values of <i>x</i> of the equation typed.</br>In order to use it properly, you have to fill all of the boxes</br>
and click <q>ok</q>
</p>
</br>
<form>
<select id="signone">
<option value="+">+</option>
<option value="-">-</option>
</select>
<input id="vara" type="text" name="firstvar" placeholder="type the coeficient a" />x<sup>2</sup>
<select id="signtwo">
<option value="+">+</option>
<option value="-">-</option>
</select>
<input id="varb" type="text" name="secondvar" placeholder="type the coeficient b " />x
<select id="signthree">
<option value="+">+</option>
<option value="-">-</option>
</select>
<input id="varc" type="text" name="thirdvar" placeholder="type the coeficient c" />=0
</form>
</br>
<button type="button" onclick="getValues();getSolution();showSolution();">Calculate</button>
<p id="showone">X1 =</p>
</br>
<p id="showtwo">X2 =</p>