1. 程式人生 > >用for; while...do; do...while; 寫出九九乘法表

用for; while...do; do...while; 寫出九九乘法表

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>

<script>
   /*
   * 用 三種迴圈語句寫出九九乘法表
   * for; while do ;do  while;
   * 
*/ /* * 方法一 * */ var oDiv1=document.getElementById('div1'); for (var nFirstNum1 = 1; nFirstNum1 < 10; nFirstNum1++) { for (var nSecNum1 = 1; nSecNum1 <= nFirstNum1; nSecNum1++) { oDiv1.innerHTML+=nFirstNum1+'*'+nSecNum1+'='+nFirstNum1*nSecNum1+'&nbsp;&nbsp;&nbsp;
'; } oDiv1.innerHTML+="<br/>"; }
/* * 方法二 * */ var oDiv2=document.getElementById('div2'); var nFirstNum2=1; while(nFirstNum2<10){ var nSecNum2=1; while(nSecNum2<=nFirstNum2){ oDiv2.innerHTML+=nFirstNum2+'*'+nSecNum2+'='+nFirstNum2
*nSecNum2+'&nbsp;&nbsp;&nbsp;'; nSecNum2++; } oDiv2.innerHTML+="<br/>"; nFirstNum2++; }
/* * 方法三 * */ var oDiv3=document.getElementById('div3'); var nFirstNum3=1; do{ var nSecNum3=1; do{ oDiv3.innerHTML+=nFirstNum3+'*'+nSecNum3+'='+nFirstNum3*nSecNum3+'&nbsp;&nbsp;&nbsp;'; nSecNum3++; }while(nSecNum3<=nFirstNum3) oDiv3.innerHTML+="<br/>"; nFirstNum3++; }while(nFirstNum3<10) </script>
</body> </html>