我正在尝试构建一个电子单元计算器。我开发了一个,但我对我的代码不满意。我在这里问你们有没有其他方法来开发这个计算器。
以下是我的代码:
HTML
<input name="" type="text" value="" onblur="check()" id="bill" />
SCRIPT
<script>
function check()
{
var a=0,b=0,c=0;
var tot = 0;
var tot1=0;
var tot2=0;
var tot3=0;
var a = document.getElementById('bill').value;
if(a>200)
{
b = a - 200;
alert('b is: '+b);
if(b>200)
{
c= b-200;
alert('c is: '+c);
tot1 = 200*4;
tot2 = 200*5;
tot3 = c*6;
tot = tot1 + tot2 + tot3;
alert(tot);
}
else
{
tot1 = 200*4;
tot2 = b*5;
tot3 = c*6;
tot = tot1 + tot2 + tot3;
alert(tot);
}
}
else
{
tot = a*4;
alert(tot);
}
}
</script>
请帮帮我朋友.. :)
答案 0 :(得分:2)
<script>
function check()
{
var a=0,b=0,c=0;
var tot = 0;
var tot1=0;
var tot2=0;
var tot3=0;
var a = parseFloat(document.getElementById('bill').value);
if(a>200)
{
tot1 = 200*4;
b = a - 200;
alert('b is: '+b);
if(b>200)
{
c= b-200;
alert('c is: '+c);
tot2 = 200*5;
tot3 = c*6;
tot = tot1 + tot2 + tot3;
alert(tot);
}
else
{
tot1 = 200*4;
tot2 = b*5;
tot3 = c*6;
tot = tot1 + tot2 + tot3;
alert(tot);
}
}
else
{
tot = a*4;
alert(tot);
}
}
</script>
<input name="" type="text" value="" onblur="check()" id="bill" />
我认为这样做:)
答案 1 :(得分:2)
您的代码需要分开 - 而且注释对于帮助其他人理解代码的用途也很有用。以下是一个实例:
function check()
{ var initialRate = 4; // electricity charged at this rate below 200
var standardRate = 5; // amount of electricity between 200 and 400 charged at this rate
var overageRate = 6; // the part of electricity usage over 400 at this rate
var electricityUsage = +document.getElementById('bill').value; // convert String to Number
if (isFinite(electricityUsage) || electricityUsage < 0) {
alert('The number entered was invalid, or was less than 0.');
return;
}
// calculates the total
alert(
electricityUsage > 400
? initialRate * 200 + standardRate * 200 + overageRate * (electricityUsage - 400)
: electricityUsage > 200
? initialRate * 200 + standardRate * (electricityUsage - 200)
: initialRate * electricityUsage
);
}
答案 2 :(得分:1)
试试这个:
function check()
{
// Algorithm
// Slabs : 200 | 400 | ...
// Amoun : 4 | 5 | 6
slabs = [200, 400]
rate = [4, 5, 6]
var total = document.getElementById('bill').value;
// Over the head
if (total > slabs[1]) {
amount = slabs[0] * rate[0] + (slabs[1] - slabs[0]) * rate[1] + (total - slabs[1]) * rate[2];
} else if (total > slabs[0]) {
amount = slabs[0] * rate[0] + (total - slabs[0]) * rate[1];
} else {
amount = total * rate[0];
}
alert("Total charge: " + amount);
}
答案 3 :(得分:1)
我想我终于拥有了它。有三个电荷波段,您需要知道每个波段的值的哪个部分。然后,您需要将适当的乘数应用于每个波段的数量。
如果我是对的,那么代码很简单:
function check() {
var a = Number(document.getElementById('bill').value);
var n = 200;
var tot1 = Math.min(n, a);
a = a - n;
var tot2 = Math.max(0, Math.min(n, a));
a = a - n;
var tot3 = Math.max(0, a);
return tot1 * 4 + tot2 * 5 + tot3 * 6;
}
如果有更多的波段,那么我们会在循环中进行计算,但对于三个波段,这就足够了。
注意:该函数返回计算的总数。
答案 4 :(得分:0)
请参阅: Sample
function check()
{
var tot = 0,tot1=0,tot2=0,tot3=0;
var a = document.getElementById('bill').value;
tot1 = (a<=200) ? (a * 4) : 800;
tot2 = (a<=400) ? ((a-200) * 5): 1000;
tot3 = (a-400) > 0 ? ((a-400) * 6): 0;
tot = tot1 + tot2 + tot3;
var msg = "Total Charge: "+tot+"\n Details\n < 200 : "+tot1;
msg += (tot2 != 0) ? "\n200 to 400 : "+tot2 : "";
msg += (tot3 != 0) ? "\n>400 : "+ tot3 : "";
alert(msg);
}