html{
font-family: monospace
;
}
正三角输出
document.write("第一种");
document.write("
<br>");
for( var i=1; i<=9; i++){ /*嵌套循环*/
for( var j=1; j<=i; j++){
document.write( j + "*" + i + "=" + i*j + (i*j<10?"
":"
"));
/*使用三目运算符使得输出的乘法表对齐*/
}
document.write("
<br>");
}
document.write("
<br><br><br>");
输出结果:
倒三角输出
document.write("第二种");
document.write("
<br>");
for( var i=9; i>=1; i--){
for( var j=i; j>=1; j--){
document.write( j + "*" + i + "=" + i*j + (i*j<10?"
":"
"));
}
document.write("
<br>");
}
document.write("
<br><br><br>");
输出结果:
反三角输出
document.write("第三种");
document.write("
<br>");
for( var i=1; i<=9; i++){
for( var k=9-i; k>=1; k--){
document.write("
");
}
for( var j=1; j<=i; j++){
document.write( j + "*" + i + "=" + i*j + (i*j<10?"
":"
"));
}
document.write("
<br>");
}
document.write("
<br><br><br>");
输出结果:
反倒三角输出
document.write("第四种");
document.write("
<br>");
for( var i=9; i>=1; i--){
for( var k=1; k<=9-i;k++){
document.write("
");
}
for( var j=i; j>=1; j--){
document.write( j + "*" + i + "=" + i*j + (i*j<10?"
":"
 "));
}
document.write("
<br>");
}
document.write("
<br><br><br>");
输出结果: