美文网首页
java-for循环

java-for循环

作者: 测试探索 | 来源:发表于2020-10-20 05:41 被阅读0次
package com.lemon.operator;

public class for_demo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        // for循环,格式
        for(int i = 1;i<=4;i++) {
            System.out.println("******");
        }
        System.out.println("循环结束");
    }
//      test
        int[] arr = {100,200,300,400,500};
        for(int i = 0;i < arr.length;i++) {
            System.out.println(arr[i]);
        }

    
}

//结果
******
******
******
******
循环结束
100
200
300
400
500
增强for循环
package com.lemon.operator;

public class for_demo {

    public static void main(String[] args) {
        int[] arr = {100,200,300,400,500};
//      增强for循环,只能循环数组和集合
//      缺点,没有索引,只能从头遍历到尾
//      优点:代码少
//      arr数组的元素是int类型,所以是int
        for(int element:arr) {
            System.out.println(element);
        }   
        System.out.println("==============");

//      arr2数组的元素是string类型,所以for循环里是String
        String[] arr2 = {"ab","11","00"};
        for(String str :arr2) {
            System.out.println(str);
        }
            
    }

}

//结果
100
200
300
400
500
==============
ab
11
00
求和思想
package com.lemon.operator;

public class ForDemo3 {

    public static void main(String[] args) {
        //求和思想
        //求1-10的和
        int sum = 0;
        for(int i =1 ;i<= 10;i++) {
            sum += i;
            System.out.println(sum);
        }
        System.out.println(sum);
    }
}
//结果
55

while循环
package com.lemon.operator;

public class whileDemo {

    public static void main(String[] args) {
        // while 输出 1-5
        int i = 1;
        while(i < 5) {
            System.out.println(i);
            i++;
        }

    }
}

//结果
1
2
3
4

相关文章

  • java-for循环

    增强for循环 求和思想 while循环

  • Java-for循环(6)

    一、for循环 基本结构: 在执行表达式1,之后进行条件的判断,如果条件成立执行循环体,如果不成立就跳出循环,在成...

  • Java-for循环打印1-100奇数

    今天来写第一个java程序,因为是初学者,有错误的地方或是更好的建议都可以跟我提出! 下面是具体的代码。。。 因为...

  • python学习笔记(2)

    循环嵌套:在循环中嵌入其他的循环体,for循环中可以嵌入for循环,while循环中嵌入while循环,for循环...

  • 循环结构

    for循环 for in循环 while循环 do while循环

  • 《每天一点Java知识》Java基础知识——循环

    循环概念 循环是在一定条件下进行循环的逻辑结构 循环组成 循环由循环入口、循环条件与循环体组成。 循环分类 已知次...

  • 4-8 循环引用

    3种循环引用 自循环引用 相互循环引用 多循环引用 Block的循环引用 NSTimer 的循环引用 破除循环引用...

  • java 温故知新 第三天

    1.循环for循环 循环次数可知 最常用while循环 循环次数非必...

  • go逻辑判断语句

    if语句 for 循环 循环嵌套 for 死循环 for 循环的分解版

  • typescript笔记(四)

    一、循环:for循环、for...in...循环、for…of 、forEach、every 和 some 循环、...

网友评论

      本文标题:java-for循环

      本文链接:https://www.haomeiwen.com/subject/gucrmktx.html