循环有三种形式:
1)while
2)do while
3)for
1.while 和2.do while
public static void main (String [] args) {
//while循环
/*int c = 0;
while(c < 10) {//如果c>10,条件不成立不会打印0出来
System.out.println(c);
c ++;
---打印的结果是0-9。
}*/
//do while 循环
int c = 0;
do{
System.out.println(c);
c ++;
if (c < 10) {//先打印一次在走循环体,条件成立退出循环体,不成立打印进入do循环
break;
}
}while (true);
}
---打印的结果是0
2.for循环
public static void text(int a) {
for(int i = 1; i <= a;i++) { // int i = 1:只走一次,进入循环体,一直到条件不成立退出循环
for(int j = 1;j <=i; j++) {
System.out.print(j+"x"+ i + "="+ i * j + "\t"); //加上ln是竖着打印完一组
}
System.out.println();
}
双for循环9*9循环表 和while循环
public static void text(int a) {
for(int i = 1; i <= a;i++) {
for(int j = 1;j <=i; j++) {
System.out.print(j+"x"+ i + "="+ i * j + "\t");
}
System.out.println();
//ln的用途是换行
}
}
public static void test2() {
//正9*9表格
int h = 1;
while(h <= 9) {
int l = 1;
while(l <= h) {
System.out.print(l + "x" + h + "="+(h * l) + "\t");
l++;
}
h ++;
System.out.println();
}
}
public static void main(String [] args) {
for(int i = 1;i <= 5 ; i++) {
for(int x = i+1;x <= 5 ;x++) {
System.out.print(" ");
}
for(int z = 1;z <= i;z++) {
System.out.print("* ");
}
System.out.println();
}
}
}