复习1:for循环嵌套的应用

本文详细介绍了Java中for循环嵌套的应用,通过实例展示了如何使用for循环在控制台上输出不同形状,并深入探讨了循环条件变化对输出形状的影响。此外,文章还演示了如何打印99乘法表及一些特殊图形,提供了丰富的编程实践案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*
for循环嵌套的应用
*/
class ForForTest2 
{
	public static void main(String[] args) 
	{
		/*
		控制台输出以下形状
		*****
		****
		***
		**
		*
		
		*/

		/*
		for(int x=0; x<5; x++)
		{
			for(int y=0; y<5; y++)
			{
				System.out.print("*");
			}
			System.out.println();
		}
		*/

		/*
		继续思考
		列是变化的
		0 5
		1 5
		2 5
		3 5
		4 5
		*/

		/*
		int z = 0;
		for(int x=0; x<5; x++)
		{
			for(int y=z; y<5; y++)
			{
				System.out.print("*");
			}
			System.out.println();
			z++;
		}
		*/

		/*
		发现z的变化和x的变化是一致的。所以不需要定义z变量了。直接使用x。
		*/
		for(int x=0; x<5; x++)
		{
			for(int y=x; y<5; y++)
			{
				System.out.print("*");
			}
			System.out.println();
		}
		
		System.out.println("--------------------");
		
		/*
		控制台输出以下形状
		*
		**
		***
		****
		*****

		*/

		//思考 0 1/2/3/4/5
		for(int x=0; x<5; x++)
		{
			for(int y=0; y<=x; y++)
			{
				System.out.print("*");
			}
			System.out.println();
		}
		
		/*
		总结:
		尖尖朝上的形状,循环条件表达式变化。
		尖尖朝下的形状,初始化表达式变化。
		*/


		System.out.println("--------------------");
		/*
		打印3以内的99乘法表
		
		1*1=1
		2*1=2 2*2=4
		3*1=3 3*2=6 3*3=9
		*/
		for(int x=1; x<=3; x++)
		{
			for(int y=1; y<=x; y++)
			{
				System.out.print(x+"*"+y+"="+x*y+"  ");
			}
			System.out.println();
		}

		System.out.println("--------------------");
		//打印99乘法表
		for(int x=1; x<=9; x++)
		{
			for(int y=1; y<=x; y++)
			{
				System.out.print(x+"*"+y+"="+x*y+"\t");
			}
			System.out.println();
		}

		//\称为转义控制字符,跟在其后的字符意义已经发生了改变

		/*
		System.out.println('\'');//输出'
		System.out.println('\n');//输出换行
		System.out.println('\t');//输出制表符 tab
		*/

		System.out.println("--------------------");

		/*
		请在控制台输出
		* * * * *
		-* * * *
		--* * *
		---* *
		----*
		*/
		
		for(int x=0; x<5; x++)
		{
			for(int y=0; y<x; y++)
			{
				//System.out.print("-");
				System.out.print(" ");
			}

			for(int y=x; y<5; y++)
			{
				System.out.print("* ");
			}
			System.out.println();
		}

		/*
		把上面的图形到这打 练习
		*/
		for (int x=1;x<=9 ;x++ )
		{
			for (int y=x;y<9 ;y++ )
			{
				System.out.print("=");
			}
			for (int c=1;c<=x ;c++ )
			{
				System.out.print("* ");
			}
			
			System.out.println();
		}

	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值