1.案例:根据《省电网销售电价表》,居民生活用电按3个梯度收费:月用电量150千瓦时及以下部分,每千瓦时0.4463元,151—400千瓦时部分为0.4663元,401千瓦时以上部分为0.5663元,请编写程序,当输入用户的用电量时,计算出所需付的费用。
分析:分为三档:401档;151-400档;150档;此三档均包含本身
2.运行结果:
3.代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script>
var ele = prompt('请输入用电量');
if (ele > 400) {
var price = (ele - 400) * 0.5663 + 250 * 0.4663 + 150 * 0.4463;
alert('401千瓦以上的计算后价格为' + price)
} else if (ele > 150) {
var price = (ele - 150) * 0.4663 + 150 * 0.4463;
alert('151-400千瓦之间的计算后价格为' + price)
} else {
var price = ele * 0.4463;
alert('150及以下的计算后价格为' + price)
}
</script>
</body>
</html>