JS输出水仙花数for循环和while循环实现
输出所有的水仙花数,水仙花数:各个位数立方和等于这个数的三位数,
//算次方的方法:Math.pow(m,n); ->m的n次方
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>水仙花数</title>
</head>
<body>
</body>
<script>
//练习:输出所有的水仙花数,水仙花数:各个位数立方和等于这个数的三位数,
//算次方的方法:Math.pow(m,n); ->m的n次方
for(var i=100;i<=999;i++){
//获得本次循环i的个位,十位,百位
var bai=parseInt(i/100);
var shi=parseInt(i%100/10);
var ge=parseInt(i%10);
if(i==Math.pow(bai,3)+Math.pow(shi,3)+Math.pow(ge,3)){
document.write(i+"<br>");
}
}
var i = 100;
while(i<=999){
var bai=parseInt(i/100);
var shi=parseInt(i%100/10);
var ge=parseInt(i%10);
if(i==Math.pow(bai,3)+Math.pow(shi,3)+Math.pow(ge,3)){
document.write(i+"<br>");
}
i++;
}
</script>
</html>
还没有评论,来说两句吧...