具体的语法就不说了,主要就是运用for循环语句。。。。
1.打印99乘法表,虽然很老套但是能帮助理解for语句.
例
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36
7 14 21 28 35 42 49
8 16 24 32 40 48 56 64
9 18 27 36 45 54 63 72 81
public class Test {
public static void main (String args[]) {
for(int i=1;i<10;i++) {
for(int j=1;j<=i;j++) {
System.out.print(i*j+" ");
}
System.out.print("\n");
}
}
}
2.打印如下图形
*
**
***
****
*****
public class ceshi {
public static void main (String args[]) {
for(int i=5;i>0;i--) {
for(int j=i;j<6;j++) {
System.out.print("*");
}
System.out.print("\n");
}
}
}
3打印如下图形
*****
****
***
**
*
public class ceshi {
public static void main (String args[]) {
for(int i=0;i<5;i++) {
for(int j=i;j<5;j++) {
System.out.print("*");
}
System.out.print("\n");
}
}
}
4.来个相对复杂的‘
* * * *
* * * *
* * * *
* * * *
* * * *
public class ceshi {
public static void main (String args[]) {
for(int i=0;i<5;i++) {
for(int j=i;j<5;j++) {
System.out.print(" ");
}
for(int z=4;z>0;z--) {
System.out.print("* ");
}
System.out.print("\n");
}
}
}
5.再来一个
*
* *
* * *
* * * *
* * * * *
public class ceshi {
public static void main (String args[]) {
int q =4;
for(int i=0;i<5;i++) {
for(int j=i;j<5;j++) {
System.out.print(" ");
}
for(int z=q;z<5;z++) {
System.out.print("* ");
}
q--;
System.out.print("\n");
}
}
}