Java中for语句生出的多重循环
public class MultipleLoops
{
public static void main(String[] args)
{
//mulLoop1();
//mulLoop2();
//mulLoop3();
//mulLoop4();
mTable();
}
//循环一
public static void mulLoop1()
{
for(int x=5;x>0;x--)//外循环5次
{
for(int y=1;y<=x;y++)//内循环变动
{
System.out.print("*");
}
System.out.println();
}
}
/*
*****
****
***
**
*
*/
//循环二
public static void mulLoop2()
{
for(int x=5;x>0;x--)
{
for(int y=1;y<=6-x;y++)
{
System.out.print("*");
}
System.out.println();
}
}
/*
*
**
***
****
*****
*/
//循环三
public static void mulLoop3()
{
for(int x=5;x>0;x--)
{
//空格内循环
for(int y=1;y<x;y++)
{
System.out.print(" ");
}
//*内循环
for(int z=1;z<=6-x;z++)
{
System.out.print("*");
}
System.out.println();
}
}
/*
*
**
***
****
*****
*/
//循环四
public static void mulLoop4()
{
for(int x=5;x>0;x--)
{
//空格内循环
for(int y=1;y<=5-x;y++)
{
System.out.print(" ");
}
//*内循环
for(int z=1;z<=x;z++)
{
System.out.print("*");
}
System.out.println();
}
}
/*
*****
****
***
**
*
*/
//根据循环二推导出久久乘法表,可观察他们的排列方式是一样的
public static void mTable()
{ //外循环可以看久久乘法表的行数
for(int x=1;x<=9;x++)
{ //内循环看久久乘法表每行数据出现的次数
for(int y=1;y<=x;y++)
{
System.out.print(" "+y+"x"+x+"="+x*y);
}
System.out.println();
}
}
}
久久乘法表:
备注:
1、循环三和四可以看做是两个内循环在执行
2、方法名:首字母小写。如果方法名由几个单词构成,那么除了第一个单词外,其余每个单词的首字母大写,其余字母小写,例如colorOfDoll。如果变量名指代的实体的数量大于一,那么采用复数形式,例如bothEyesOfDoll、allChildren
3、类名:首字母大写。如果类名由几个单词构成,那么每个单词的首字母大写,其余字母小写,例如MultipleLoops