import java.util.Scanner;
public class PassWord{
public static void main(String[] args) {
int password=112345;//正确密码
int i=0;
Scanner scanner=new Scanner(System.in);
System.out.println("enter password:");//开始输入密码
while(i<3){
int num=scanner.nextInt();
if(num!=password){//密码输入错误
i++;
if(i==3){//3次输入错误,则提示退出程序
System.out.println("eixt program commond");
break;
}
System.out.println("wrong! please enter again:");
}
else if(num==password){//密码输入正确
System.out.println("Congratulations!");
break;
}
}
scanner.close();
}
}
三次输入错误:
输入成功: