do while 简介
do while 细节
public class DoWhileExercise01 {
//编写一个main方法
public static void main(String[] args) {
//统计1---200之间能被5整除但不能被3整除的 个数
//化繁为简
//(1) 使用do-while输出 1-200
//(2) 过滤 能被5整除但不能被3整除的数 %
//(3) 统计满足条件的个数 int count = 0;
//先死后活
//(1) 范围的值 1-200 你可以做出变量
//(2) 能被5整除但不能被3整除的 , 5 和 3 可以改成变量
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);
}
}
import java.util.Scanner;
public class DoWhileExercise02 {
//编写一个main方法
public static void main(String[] args) {
//如果李三不还钱,则老韩将一直使出五连鞭,直到李三说还钱为
//[System.out.println("老韩问:还钱吗?y/n")] do...while ..
//
//化繁为简
//(1) 不停的问还钱吗?
//(2) 使用char answer 接收回答, 定义一个Scanner对象
//(3) 在do-while 的while 判断如果是 y 就不在循环
//一定自己动脑筋..
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("李三还钱了");
}
}