javascript的一个找零的小程序
javascript的一个找零的小程序
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script type="text/javascript">
function check(){
var sum=document.form.one.value;
if(sum>100||sum<1){
alert("请输入1~100之间的数!");
return null;
}
var a=0,b=0,c=0,d=0,e=0;
while(sum>=50){
sum-=50;
a++;
}
while(sum>=20){
sum-=20;
b++;
}
while(sum>=10){
sum-=10;
c++;
}
while(sum>=5){
sum-=5;
d++;
}
while(sum>=1){
sum-=1;
e++;
}
var str="应找零钱为:";
var flag=0;
if(a!=0){
flag=1;
str+="50元" + a + "张";
}
if(b!=0){
if(flag){
str+=",";
}
flag=1;
str+="20元" + b + "张";
}
if(c!=0){
if(flag){
str+=",";
}
flag=1;
str+="10元" + c + "张";
}
if(d!=0){
if(flag){
str+=",";
}
flag=1;
str+="5元" + d + "张";
}
if(e!=0){
if(flag){
str+=",";
}
flag=1;
str+="1元" + e + "张";
}
str+="。";
alert(str);
}
</script>
</head>
<body>
<form name="form">
请输入找零的数量(数值在0~100之间): <input name="one" type="text" >
<input type="button" value="提交" onclick="check()">
</form>
</body>
</html>