Java语言基础(for语句循环嵌套)

本文介绍了Java中的循环嵌套,特别是使用for语句实现九九乘法表的练习。同时,详细讲解了转义字符的概念,包括常见的转义字符如换行符( )、制表符( )等,并通过示例展示了如何在字符串中使用它们。此外,还讨论了流程控制语句中的continue关键字,说明了其在循环结构中的作用。

嵌套

class ForForDemo   
{
	public static void main(String[] args)
	{
		//大圈套小圈思想:对于一种重复情况的每一次重复都对应着另外一种情况的多种情况
		for(int x=0;x<3;x++)  //外循环
		{
			for(int y=0;y<4;y++)  //内循环 先完成内循环
			{
				System.out.println("x="+x);
			}
		}
	}

}

class ForForDemo   
{
	public static void main(String[] args)
	{
	/*
	*****
	*****
	*****
	*****
	*/
		for(int x=0;x<4;x++)  //总共四行,所以定义x让其循环四次
		{
			for(int y=0;x<5;y++)  //每行有五个,所以定义y让每行里的单项进行循环
				{
				System.out.print("*");
				}
		System.out.println();  //换成四行
		}
	}
}

练习

class ForForDemo   
{
	public static void main(String[] args)
	{
	/*
	*****
	****
	**
	*
	*/
		int z=5;
		for(int x=0;x<5;x++)  //1-5 1-4 1-3 1-2 1-1
		{
			for(int y=0;y<z;y++) 
				{
				System.out.print("*");
				}
		System.out.println();
		z--;
		}
		/*
		或者
		int z=0;
		for(int x=0;x<5;x++)  //1-5 2-5 3-5 4-5 5-5
		{
			for(int y=z;y<5;y++) 
				{
				System.out.print("*");
				}
		System.out.println();
		z++;
		*/
		/*
		或者
		int z=0;
		for(int x=0;x<5;x++)  //1-5 2-5 3-5 4-5 5-5
		{
			for(int y=x;y<5;y++) 
				{
				System.out.print("*");
				}
		System.out.println();
		*/
	}
}
class ForForDemo   
{
	public static void main(String[] args)
	{
	/*
	
	* * * * *
	-* * * *
	--* * *
	---* *
	----*
	
	*/
		for (int x=1;x<=5;x++)
		{
			for(int y=1;y<=x;y++)   //三角形尖朝上 y<=x
			{
				System.out.print("_");
			}
			for(int z=x;z<=5;z++)  //三角形尖朝下 z=x
				{
					System.out.print("* ");
				}
		System.out.println();
		}


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

		/*
		*****
		****-
		***- -
		**- - -
		*- - - -
		*/
	}
}

练习九九乘法表&转义字符

class ForFor99Demo   
{
	public static void main(String[] args)
	{
	/*
	练习九九乘法表
	1*1=1
	1*2=2 2*2=4
	1*3=3 2*3=6 3*3=9
	1*4=4
	1*5=5
	1*6=6
	1*7=7
	1*8=8
	1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
	*/
	for(int x=1;x<=9;x++) //总共九行
		{
			for(int y=1;y<=x;y++) //每行运算x次
				{
					System.out.print(x+"*"+y+"="+x*y+"\t");
				}
			System.out.println();
		}
	}
}

简单练习

转义字符:能转变某些字母或者符号含义的字符

转义字符 \ 用在字符串当中
\n 换行(回车符)
\t 制表符
\b 退格
\r 按下回车键 在windows系统中回车符是由两个符号组成的 \r \n
在linux中 回车符就是\n

System.out.println ("“hello word”")
System.out.println ("\hello word\")

其它流程控制语句

其它流程的控制语句

break 跳出; continue 继续;
continue语句:应用于循环结构
break的作用范围:要么是switch语句,要么是循环语句。
离不开应用范围,单独存在是没有意义的。
当break语句单独存在时,在同一大括号下面不要定义其它语句,执行不到

class BreakContinueDemo   
{
	public static void main(String[] args)
	{
	/*
	break:跳出
	break的作用范围:要么是switch语句,要么是循环语句。
	当break语句单独存在时,在同一大括号下面不要定义其它语句,执行不到。
	break跳出的是所在的当前循环。如果出现的循环嵌套的情况,break想要跳出指定的循环,可以通过标号来完成。
	*/
		for (int x=0;x<3;x++)
		{
			System.out.println ("x="+x);
			break;   //直接跳出循环 
		}
	}
}

continue

continue:继续
作用范围:只作用于循环结构。
continue:如果符号附加条件,则结束本次循环,进行下次循环,如果不符合附加条件,就继续进行循环。
如果continue单独存在时,下面不要有任何语句,因为无法执行(执行不到)。

for (int x=0;x<3;x++)
{
   if (x%2==0)
   	continue;
   System.out.println ("x="+x);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值