当前位置:网站首页>通过js来实现一元二次方程的效果,输入a,b,c系数后可计算出x1和x2的值
通过js来实现一元二次方程的效果,输入a,b,c系数后可计算出x1和x2的值
2022-07-29 04:12:00 【小杨要努力哇】
目录
第四步:接下来我们通过判断 △的情况,来判断有无实根,所以我们这里先定义一下△(disp)
第五步:接下来我们通过判断 △的大小,当△小于0时,方程无实根
第一步:框架
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script>
</script>
</body>
</html>
第二步:我们知道,一元二次方程的表达式为
求根公式为

所以这里我们要先定义x1,x2.a,b,c,△,disp
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script>
var x1, x2, a, b, c, disp
</script>
</body>
</html>
第三步:我们定义三个变量用来接收a,b,c
<!DOCTYPE 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>
第四步:接下来我们通过判断 △的情况,来判断有无实根,所以我们这里先定义一下△(disp)
<!DOCTYPE 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()是定义平方的一个函数
第五步:接下来我们通过判断 △的大小,当△小于0时,方程无实根
<!DOCTYPE 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>
第六步:当△等于0时x1等于x2
<!DOCTYPE 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>
第七步:当△大于0时,
<!DOCTYPE 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()是根号的意思
边栏推荐
- 【深度学习CPU(番外篇)——虚拟内存】
- 数据源是SQL server ,我要配置日期字段 updateDate 最后两天日期的增量数据,做增
- Copy products with one click from Taobao, tmall, 1688, wechat, jd.com, Suning, taote and other platforms to pinduoduo platform (batch upload baby details Interface tutorial)
- SQL语句 关于字段转换怎么写
- [原理] 横向渗透的几种方式
- Is the browser multi process or single process?
- rman不标记过期备份
- Rhel8 patch package production
- Function pointer and callback function
- 当我从数据库获取到了winfrom特定的控件ID之后我需要通过这个ID找到对应的控件,并对控件的TEXT文本进行赋值这该怎么做
猜你喜欢

How to solve the problem of store ranking?

Locally call tensorboard and Jupiter notebook on the server (using mobaxterm)

Design of environment detection system based on STM32 and Alibaba cloud

店铺排名问题,如何解决?
![[deep learning CPU (part outside) - virtual memory]](/img/f7/4c72d583456f6f68c52424602ff5d9.png)
[deep learning CPU (part outside) - virtual memory]

Is the array name a pointer

安装ros的laser_scan_matche库所遇到的问题(一)

Nacos registry

UnicodeDecodeError: ‘ascii‘ codec can‘t decode byte 0x90 in position 614: ordinal not in range(128)

Interview essential! TCP classic 15 consecutive questions!
随机推荐
Applet: Area scrolling, pull-down refresh, pull-up load more
SQL server how to judge when the parameter received by the stored procedure is of type int?
Blood cases caused by < meta charset=UTF-8> -- Analysis of common character codes
[kvm] create virtual machine from kickstart file
SQL语句 关于字段转换怎么写
基于STM32和阿里云的环境检测系统设计
C language: getchar () and cache
Asp.net MVC中文件夹中的控制器如何跳转到根目录的控制器中?
初识C语言(3)
[introduction to C language] zzulioj 1031-1035
GBase 8a特殊场景下屏蔽 ODBC 负载均衡方式?
Rhel8 patch package production
Zhihuijun, a genius of Huawei, made a modular mechanical keyboard, which caused an earthquake in the geek circle. Netizens: This is the real customization
通过PWM呼吸灯和PWM控制直流电机来详细介绍TIM的输出比较功能
为什么opengauss启动的时候这么多的unknown?
Do you have a boss to help me check whether the parameter configuration of the Flink SQL connection Kafka authentication Kerberos is wrong
[principle] several ways of horizontal penetration
Object detection: object_ Detection API +ssd target detection model
C language to achieve three chess game (detailed explanation)
Is the browser multi process or single process?
