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