Java-学习篇-06-循环

Java中循环语句

主要有for循环和while循环,while循环又有两种分别是while和do…while。
for循环可能会更常见一点。

for

基本结构:

for(初始语句; 判断语句; 后置语句){
	循环体
}
  • 任务1:打印1到10的整数
public class CirculationTest {
    public static void main(String[] args){
        for (int i = 1; i <= 10; i++) {
            System.out.print(i);
            System.out.print("\t");
        }
    }
}

输出

1	2	3	4	5	6	7	8	9	10	
  • 任务2:打印1到10的偶数
public class CirculationTest {
    public static void main(String[] args){
        for (int i = 1; i <= 10; i++) {
            if (i % 2 == 0) {
                System.out.print(i);
                System.out.print("\t");
            }
        }
    }
}

输出

2	4	6	8	10	
  • 任务3:打印九九乘法表
public class CirculationTest {
   public static void main(String[] args) {
       for (int i = 1; i < 10; i++) {
           for (int j = i; j < 10; j++) {
               System.out.print(i + "x" + j + "=" + i * j + "\t");
           }
           System.out.println();
       }
   }
}

输出

1x1=1	1x2=2	1x3=3	1x4=4	1x5=5	1x6=6	1x7=7	1x8=8	1x9=9	
2x2=4	2x3=6	2x4=8	2x5=10	2x6=12	2x7=14	2x8=16	2x9=18	
3x3=9	3x4=12	3x5=15	3x6=18	3x7=21	3x8=24	3x9=27	
4x4=16	4x5=20	4x6=24	4x7=28	4x8=32	4x9=36	
5x5=25	5x6=30	5x7=35	5x8=40	5x9=45	
6x6=36	6x7=42	6x8=48	6x9=54	
7x7=49	7x8=56	7x9=63	
8x8=64	8x9=72	
9x9=81	

while

do…while

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值