老师每天检查小明的学习任务是否合格,如果不合格则继续进行,学习任务为:上午阅读教材,下午上机编程。
public static void main(String[] args) {
String next;
Scanner sc=new Scanner(System.in);
System.out.println("合格了吗");
next = sc.next();
while("不合格".equals(next)) {
System.out.println("上午阅读教材");
System.out.println("下午上机编程");
System.out.println("合格了吗");//每次循环完继续问,直到合格为止。
next = sc.next();
}
System.out.println("恭喜学习任务合格");
sc.close();
}
do-while循环:
特点:先执行,再判断
这天老师给小明出了一道测试题,让他上机编写程序完成,老师来查看是否合格,如果不合格则继续编写..
public static void main(String[] args) {
String next;
Scanner sc=new Scanner(System.in);
do {
System.out.println("上机编写程序!");
System.out.println("是否合格?");
next=sc.next();
}while("不合格".equals(next));
System.out.println("恭喜通过测试!");
}