while和do while循环
package Basic;
public class Test2 {
public static void main(String[] args) {
while (true){//while死循环
System.out.println("浩哥牛X");
}
}
}
package Basic;
public class Test2 {
public static void main(String[] args) {
int i = 0;
while (i < 5){
System.out.println("浩哥牛X");
i++;
}
}
}
浩哥牛X
浩哥牛X
浩哥牛X
浩哥牛X
浩哥牛X
Process finished with exit code 0
package Basic;
import java.util.Scanner;
public class Test2 {
public static void main(String[] args) {//运用死循环实现监听输入,符合条件返回相应值或退出。
Scanner scanner = new Scanner(System.in);
int i = 0;
flag:while (i != 5){
System.out.println("请输入一个数字:");
i = scanner.nextInt();
if(i>8){
System.out.println("浩哥牛X");
}else if (i==5){//break 返回 flag
break flag;
}else {
System.out.println("输错了!快重新输入!");
}
}
System.out.println("程序正常退出!");
}
}
请输入一个数字:
4
输错了!快重新输入!
请输入一个数字:
9
浩哥牛X
请输入一个数字:
5
程序正常退出!
Process finished with exit code 0
package Basic;
import java.util.Scanner;
public class Test2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一个数字:");
int i = scanner.nextInt();
while (i > 5) {//while是先判断满足条件后执行,当不满足条件时不在执行
System.out.println("while:");
i--;
}
System.out.println("============================================");
do {//do while是先执行再判断,满足条件再继续执行。不满足条件不再执行,这时结果执行了一次。
System.out.println("do while:");
}while (i>10);
}
}
请输入一个数字:
6
while:
============================================
do while:
Process finished with exit code 0