栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

循环语句

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

循环语句

循环结构 while

while(布尔型表达式){

//执行语句

}

package com.xujing.scanner;
​
public class WhileDemo01 {
    public static void main(String[] args) {
        int i=1;
        int s=0;
        while(i<=100){
            s+=i;
            i++;
        }
        System.out.println(s);
    }
}
​
do while

do{//执行语句

}

while(布尔型表达式);

package com.xujing.scanner;
​
public class DoWhileDemo01 {
    public static void main(String[] args) {
        int i=1;
        int s=0;
        do{
            s+=i;
            i++;
        }while (i<=100);
        System.out.println(s);
    }
}
​
for

for(初始值;布尔值表达式;更新){

//执行语句

}

package com.xujing.scanner;
​
public class ForDemo01 {
    public static void main(String[] args) {
        int s=0;
        for (int i = 0; i <=100; i++) {
            s+=i;
        }
        System.out.println(s);
    }
}
​
package com.xujing.scanner;
​
public class ForDemo02 {
    public static void main(String[] args) {
        int jis=0;
        int ous=0;
        for (int  i= 0;  i<= 100; i++) {
            if(i%2==0){
                ous+=i;
            }
            else{
                jis+=i;
            }
​
        }
        System.out.println(ous);
        System.out.println(jis);
    }
}
​
package com.xujing.scanner;
​
public class ForDemo03 {
    public static void main(String[] args) {
        for (int i = 0; i <= 1000; i++) {
            if(i%5==0){
                System.out.print(i+"   ");
            }
            if(i%15==0){
                System.out.println();
            }
        }
    }
}
​
package com.xujing.scanner;
​
public class ForDemo04 {
    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j+"*"+i+"="+(i*j)+"   ");
            }
            System.out.println();
        }
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/271871.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号