1.do ... difference while the while loop statement
while loop
First determine, after executing the statement. Before the statement is executed, it must first control loop.
public class Demo {
public static void main(String[] args) {
int i=0,j=0;
System.out.println("befor while j="+j);
while(i>0) {
j++;
}
System.out.println("after while j="+j);
}
}
The results as shown below:
do ... while loop
To execute the statement after the judgment. Before executing the statement, no need to consider the control loop.
public class Demo {
public static void main(String[] args) {
int i=0,j=0;
System.out.println("befor while j="+j);
do {
j++;
}while(i>1);
System.out.println("after while j="+j);
}
}
The results as shown below:
2.do ... while the password twice to achieve parity
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
String pwd1;
String pwd2;
do {
System.out.println("请输入密码:");
pwd1=input.nextLine();
System.out.println("请再次输入密码:");
pwd2=input.nextLine();
if(!pwd1.equals(pwd2)) {
System.out.println("您输入的密码不一致,请重新输入!");
}
System.out.println();
}while(!pwd1.equals(pwd2));
System.out.println("密码设置成功!");
}
}
The results as shown below: