内容纲要
126讲while、do while
- while循环流程图
输出10句
“你好韩顺平教育”
public class While01{
public static void main(String[] args){
int i = 1;
while(i <=10){
System.out.println("hello,DearL" + i);
i++;//如果没有这个变量迭代,会编程一个无限循环(死循环)。
}
System.out.println("推出while,继续。。");
}
}
while循环控制
public class WhileExercise{
public static void main(String[] args){
//思路分析:打印1-100之间所有能被3整除的数,使用while语句
//1.初始化一个int 变量 a, 判断a % 3 == 0 ?,为0就打印输出
int a = 0;
while(a <=100){
if(;a%3 == 0;){
System.out.println("diliver 3 is a Integar =" + a);
}
a++;
}
System.out.println("done");
System.out.println("\t ======================");
//打印40-400之间所有的偶数,使用while练习
//化繁为简,先死后活
//思路分析
//1.先打印40-200之间的所有偶数
int b = 40;
while(b <= 200){
if(b%2 == 0){
System.out.println("b=" + b);
}
b++;
}
System.out.println("Done");
}
}
do..while循环
要账
while循环:先问还不还钱?不还钱打一顿,再问还不还钱?直到还钱就不打了。
do..while循环:先打一顿,再问还不还钱?不还钱再打一顿,直到还钱才不打
- do while流程图
- 练习题:用do while输出10句Hello,DearL
public class DoWhile{
public static void main(String[] args){
int a = 1;
do{
System.out.println("Hello,DearL" + a);
a++;
}while(a <= 10);
}
}
课堂练习
public class DoWhileExercise{
public static void main(String[] args){
int a = 1;
int sum = 0;
do{
System.out.println(a);
++a;
sum += a;
}while(a <= 100);
System.out.println("sum=" + sum);
}
}
public class DoWhileExercise01{
public static void main(String[] args){
int min = 1;
int max = 200;
int count = 0;
int num = 0;
do{
if((count%5 == 0) && (count%3 != 0)){
System.out.println("num =" + count);
num++;
}
count++;
}while(count <= max);
System.out.println("num =" + num);
}
}
import java.util.Scanner;
public class DoWhileExercise02{
public static void main(String[] args){
Scanner myScanner = new Scanner(System.in);
char answer = ' ';
do{
System.out.println("fxxxu");
System.out.println("give back money ? Y/N?");
answer = myScanner.next().charAt(0);
}while(answer != 'Y');
System.out.println("done");
}
}
//判断条件很关键!!! while(answer != 'y');