目录
第四步:接下来我们通过判断 △的情况,来判断有无实根,所以我们这里先定义一下△(disp)
第五步:接下来我们通过判断 △的大小,当△小于0时,方程无实根
- html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>title>
- head>
- <body>
- <script>
-
- script>
- body>
- html>
所以这里我们要先定义x1,x2.a,b,c,△,disp
- html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>title>
- head>
- <body>
- <script>
- var x1, x2, a, b, c, disp
-
- script>
- body>
- html>
- html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>title>
- head>
- <body>
- <script>
- var x1, x2, a, b, c, disp
- var a = prompt('请输入a')
- var b = prompt('请输入b')
- var c = prompt('请输入c')
-
- script>
- body>
- html>
- html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>title>
- head>
- <body>
- <script>
- var x1, x2, a, b, c, disp
- var a = prompt('请输入a')
- var b = prompt('请输入b')
- var c = prompt('请输入c')
- disp = (Math.pow(b, 2) - (4 * a * c))
-
- script>
- body>
- html>
这里Math.pow()是定义平方的一个函数
- html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>title>
- head>
- <body>
- <script>
- var x1, x2, a, b, c, disp
- var a = prompt('请输入a')
- var b = prompt('请输入b')
- var c = prompt('请输入c')
- disp = (Math.pow(b, 2) - (4 * a * c))
-
- if (disp < 0) {
- alert('抱歉,方程无实根')
- }
- script>
- body>
- html>
- html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>title>
- head>
- <body>
- <script>
- var x1, x2, a, b, c, disp
- var a = prompt('请输入a')
- var b = prompt('请输入b')
- var c = prompt('请输入c')
- disp = (Math.pow(b, 2) - (4 * a * c))
-
- if (disp < 0) {
- alert('抱歉,方程无实根')
- } else if (disp == 0) {
- x1 = (-b / 2 * a)
- x2 = (-b / 2 * a)
- alert('x1=x2值为' + x1)
- }
- script>
- body>
- html>
- html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>title>
- head>
- <body>
- <script>
- var x1, x2, a, b, c, disp
- var a = prompt('请输入a')
- var b = prompt('请输入b')
- var c = prompt('请输入c')
- disp = (Math.pow(b, 2) - (4 * a * c))
-
- if (disp < 0) {
- alert('抱歉,方程无实根')
- } else if (disp == 0) {
- x1 = (-b / 2 * a)
- x2 = (-b / 2 * a)
- alert('x1=x2值为' + x1)
- } else {
- x1 = ((-b + Math.sqrt(disp)) / 2 * a)
- x2 = ((-b - Math.sqrt(disp)) / 2 * a)
- }
- alert('x1的值为' + x1 + '\n' + 'x2的值为' + x2);
- script>
- body>
- html>
这里的Math.sqrt()是根号的意思