循环

循环有三种形式:

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();
		}
		
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值