1、do-while循环:
a.
是
while
循环的一个变种
.
b.
语句语法结构
do{
循环体
;
}while(
循环继续条件
);
c.
和
while
的区别
:
while
循环是先判断循环条件
,
再执行循环体
do-while.
先执行一次循环体
,
再判断循环条件
.
d.
一般情况下
,
使用
while
或
do-while
循环的时候
,
都会和循环控制变量联合使用
.
而定义循环控制变量的时候
,boolean
类型的变量一般的默认值初始值是
false.
e.
到底如何选择使用
while
还是
do-while
循环
.
任意
.
2、实例代码:
package main.java.Result.DataStructure;
import java.util.Scanner;
/**
* 简单的菜单选择系统,用do-while循环实现。
* @author 晴澜
* @version 2023.03.17
*/
public class Menu {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
Boolean isGoon=false;
do{
System.out.println("\txxxxx银行");
System.out.println("1.存款");
System.out.println("2.取款");
System.out.println("3.转账");
System.out.println("4.查询余额");
System.out.println("5.退卡");
System.out.print("请选择:");
String str = sc.next();
switch (str){
case "1":
isGoon = true;
System.out.println("欢迎来到存款");
break;
case "2":
isGoon = true;
System.out.println("取款");
break;
case "3":
isGoon = true;
System.out.println("转账");
break;
case "4":
isGoon = true;
System.out.println("查询余额");
break;
case "5":
isGoon = false;
System.out.println("退出");
break;
default:
isGoon = true;
System.out.println("输入错误");
}
}while (isGoon);
}
}
长风破浪会有时,直挂云帆济沧海。