while 先判断在执行不满足条件一次都不执行
do while 先做、判断、再执行,不管任何情况至少执行一遍
package pinduoduo;
import java.util.Scanner;
public class while_xunhuan {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String answer;
System.out.println("合格吗?(yes/no)");
answer = input.next();
//或者while("no.equals(answer)")
while (!"yes".equals(answer)) {
System.out.println("上午阅读教材");
System.out.println("下午上机编程");
System.out.println("合格吗(yes/no)");//很重要,保证有机会跳出循环
answer =input.next();
}
}
}
package pinduoduo;
import java.util.Scanner;
public class doWhille_xunhuan {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double answer;
do {
System.out.println("上机编写程序");
System.out.println("你的成绩是:");
answer =input.nextDouble();
} while (answer<70);
System.out.println("恭喜通过测试");//合格跳出循环
}
}