一、基本语法
循环变量初始化;
do{
循环体(语句);
循环变量迭代;
}while(循环条件);
说明:
1、do while 是关键字
2、也有循环四要素,只是位置不一样
3、先执行,再判断,也就是说,一定会至少执行一次
4、最后有一个分号;
5、while和do..while区别举例:要账问题
二、注意事项和细节说明
1、循环条件是返回一个布尔值的表达式
2、do..while循环是先执行,再判断,因此他至少执行一次
三、案例说明
public class DoWhile02 {
public static void main(String[] args) {
int i = 1;
int sum = 0;
do{
System.out.println(i);
sum += i;
i ++;
}while(i <= 100);
System.out.println("总和="+ sum);
}
}
package com;
public class DoWhile03 {
public static void main(String[] args) {
int i = 1;
int count = 0;
do {
if( i % 5 ==0 && i % 3 !=0 ){
System.out.println("i=" + i);
count ++;
}
i ++;
}while(i <= 200);
System.out.println("count=" + count);
}
}
package com;
import java.util.Scanner;
public class DoWhile04 {
public static void main(String[] args) {
Scanner myScanner = new Scanner(System.in);
char answer = ' ';
do{
System.out.println("老王使出五连鞭!");
System.out.println("还钱不你?y/n");
answer = myScanner.next().charAt(0);
System.out.println("张三的回答是;"+answer);
}while (answer != 'y');
System.out.println("张三还钱了");
}
}