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 循环执行结束,可以休息一下了");
    }
}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Java中for、while、do-while循环的基本流程: 1. for循环:for循环是一种常用的循环结构,它的语法如下: ```java for (初始化; 判断条件; 更新操作) { // 循环体 } ``` 其中,初始化语句只会在循环开始时执行一次,判断条件会在每次循环开始前进行判断,如果为true则执行循环体,否则跳出循环,更新操作会在每次循环结束后执行。 2. while循环:while循环是一种基本的循环结构,它的语法如下: ```java while (判断条件) { // 循环体 } ``` 其中,判断条件会在每次循环开始前进行判断,如果为true则执行循环体,否则跳出循环。 3. do-while循环:do-while循环是一种先执行循环体再判断条件的循环结构,它的语法如下: ```java do { // 循环体 } while (判断条件); ``` 其中,循环体会在第一次循环开始前执行一次,然后在每次循环开始前进行判断,如果为true则继续执行循环体,否则跳出循环。 以下是一个Java程序,使用while循环来进行猜数的过程: ```java import java.util.Scanner; import java.util.Random; public class GuessNumber { public static void main(String[] args) { Scanner input = new Scanner(System.in); Random random = new Random(); int number = random.nextInt(100) + 1; int guess; int count = 0; boolean win = false; System.out.println("猜数游戏开始!"); while (count < 5) { System.out.print("请输入一个1到100之间的整数:"); guess = input.nextInt(); count++; if (guess == number) { win = true; break; } else if (guess < number) { System.out.println("你猜的数太小了!"); } else { System.out.println("你猜的数太大了!"); } } if (win) { System.out.println("恭喜你,猜对了!"); } else { System.out.println("很遗憾,你没有猜对。正确答案是:" + number); } } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值