今天练习for语句的使用,先是简单的例子:
/*
打印如下图形:
******
******
******
******
*/class ForForDemo
{
public static void main(String[] args)
{
for(int x=0;x<4;x++){
for (int y=0;y<6 ;y++)
{
System.out.print("*"); //打印一行的星星
}
System.out.println(); //打印完一行之后,打印回车键
}
}
}
/*
tips:
1. 双层嵌套for语句,第一个for语句代表几行,第二个for语句代表几列
*/
这个比较简单,下面是稍微复杂一点的:
/*
*****
****
***
**
*
*/
class ForForTest1
{
public static void main(String[] args)
{
for (int x=0;x<5 ;x++ )
{
for (int y=0;y<5-x ;y++ )
{
System.out.print("*");
}
System.out.println();
}
}
}