欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

javafor,while,dowhile循环三种概念

时间:2023-07-04

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

 

 

 

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。