Blogger Information
Blog 20
fans 0
comment 0
visits 25254
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
javascript使用三种循环语句输出九九乘法表与输出一个三角形
左手Leon的博客
Original
2262 people have browsed it

实例

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>js打印乘法表与输出三角形</title>
	<script type="text/javascript">
		document.write('for循环输出九九乘法表:');
		for (var i = 1; i <=9 ; i++) {
			for(var j=i;j<=9;j++){
				var n;
				if(i*j>=10){
					n='  ';
				}else{
					n='    ';
				}
				document.write(i+'X'+j+'='+(i*j)+n);
			}
			document.write('<br>');
		}
		document.write('<hr>');
		// unset(i);
		// unset(j);

		document.write('while循环输出九九乘法表:');
		document.write('<br>');
		var k=1,l,m;
		while(k<=9){
			for(l=k;l<=9;l++){
				
				if(k*l>=10){
					m='  ';
				}else{
					m='    ';
				}
				document.write(k+'X'+l+'='+(k*l)+m);
			}
			document.write('<br>');
			k++;
		}
		document.write('<hr>');

		document.write('do while循环输出九九乘法表:');
		document.write('<br>');
		var a=1,b=1,c;
		do{
			for(b=a;b<=9;b++){
				
				if(a*b>=10){
					c='  ';
				}else{
					c='    ';
				}
				document.write(a+'X'+b+'='+(a*b)+c);
			}
			document.write('<br>');
			a++;
			// while(b<=9){
			// 	b=a;
			// 	if(a*b>=10){
			// 		c='  ';
			// 	}else{
			// 		c='    ';
			// 	}
			// 	document.write(a+'X'+b+'='+(a*b)+c);
			// }
			// document.write('<br>');
			// a++;   
		}while(a<=9)
		document.write('<hr>');
		
		document.write('for循环输出三角形:');
		document.write('<hr>');
		for(var e=1;e<=10;e++){
			for(var f=10-e;f>=1;f--){
				document.write('&nbsp');
			}
			for(var g=1;g<=e;g++){
				document.write('*');
			}
			document.write('<br>');
		}
		document.write('<hr>');

		document.write('while循环输出三角形:');
		document.write('<br>');
		var e=1,f,g;
		while(e<=10){
			for(var f=10-e;f>=1;f--){
				document.write('&nbsp');
			}
			for(var g=1;g<=e;g++){
				document.write('*');
			}
			document.write('<br>');
			e++;
		}

	</script>
</head>
<body>
	
</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例


学习心得:

在循环嵌套中,除了for以外,while嵌套不能实现

Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post