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

java for,while,do while循环三种概念

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

java for,while,do while循环三种概念

 while循环

public class SettleAccountUsingWhile {
    public static void main(String[] args) {
        int times = 5;
        while (times > 0) {
            int price = 5;
            int amount = 10;
            if (price > 0 && amount > 0) {
                int totalCost = price * amount;
                System.out.println(totalCost);
            } else {
                System.out.println("price和amount的值必须都大于0,否则无法计算");
            }
            times = times - 1;
        }
        System.out.println("while循环语句执行结束,结账员可以休息一下了。");
    }
}

for循环

public class SettleAccountUsingfor {
    public static void main(String[] args) {
        for (int times = 0; times <= 5; times++) {
            int price = 5;
            int count = 10;
            if (price > 0 && count > 0) {
                int totalCost = price * count;
                System.out.println(totalCost);
            } else {
                System.out.println("当price和count同时大于0时,才可以执行for循环");
            }
        }
        System.out.println("for循环执行结束,工作人员可以休息了");
    }
}

do while循环

public class SettleAccountUsingdowhile {
    public static void main(String[] args) {
        int times = 5;
        do {
            int price = 5;
            int count = 10;
            if (price > 0 && count > 0) {
                int totalprice = price * count;
                System.out.println(totalprice);
            } else {
                System.out.println("price和count的值必须都大于0,否则执行不了do while 循环");
            }
            times = times - 1;
        } while (times > 0);
        System.out.println("do while 循环执行结束,可以休息一下了");
    }
}

 

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/732501.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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