编写代码模拟三次密码输入的场景。 最多能输入三次密码,密码正确,提示“登录成功”,密码错误, 可以重新输
入,最多输入三次。三次均错,则提示退出程序。
import java.util.Scanner;
public class test {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int count=3;
while(count!=0){
System.out.println("请输入password:");
String password=sc.nextLine();
if("123456".equals(password)){
System.out.println("登陆成功");
break;
}
else{
count--;
System.out.printf("你还有%d次机会",count);
}
}sc.close();
}
}