for循环嵌套:
class ForForDemo{
public void static main(String[] args){
for(int x = 0; x <= 4 ; x ++){//外循环控制的是行数
for(int y = 0 ; y <= 4 ; y ++){//内循环控制的是每行的个数
System.out.print("*");
}
System.out.println( );
}
}
}
*****
****
***
**
*
class ForForDemo{
public void static main(String[] args){
int z = 5;
for(int x = 0; x <= 4; x ++){
for(int y = 0 ; y <= 5-x ; y ++){
System.out.print("*");
}
System.out.println( );
z --;
}
}
或者:
class ForForDemo{
public void static main(String[] args){
for(int x = 1; x <= 5; x ++){
for(int y = x ; y <= 5 ; y ++){
System.out.print("*");
}
System.out.println( );
}
}
*
**
***
****
*****
class ForForDemo2{
public void static main(String[] args){
for(int x = 1 ;x <= 5 ;x ++){
for(int y = 1 ; y < = x; y ++){
System.out.print("*");
}
System.out.println( );
}
}
}
54321
5432
543
54
5
class ForForDemo3{
public void static main(String[] args){
for(int x = 1 ; x <= 5 ; x ++){
for( int y = 5 ; y >= x ; y-- ){
System.out.print(y);
}
System.out.println( );
}
}
}
1
22
333
4444
55555
class ForForDemo4{
public void static main(String[] args){
for(int x = 1; x <= 5 ; x ++){
for(int y = 1 ; y <= x ; y++){
System.out.print(x);
}
System.out.println( );
}
}
}
打印九九乘法表:
class ForForDemo5{
public void static main(String[] args){
for(int x = 1 ; x <= 5 ; x ++){
for(int y = 1; y <= x ;y ++){
System.out.print(x + "*" + y + "=" + (x*y) + "/t");
}
}
}
}