具体要求:
编写代码模拟三次密码输入的场景。 最多能输入三次密码,密码正确,提示“登录成功”,密码错误, 可以重新输 入,最多输入三次。三次均错,则提示退出程
import java.util.Scanner;
public class LongIn {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int count=3;
while(count!=0){
System.out.println("请输入密码") ;
String password=scanner.next();//用next或者nextline读取密码
if(password.equals("123456")) {
//要注意在Java中比较字符串是否相同,使用equals而不是=
System.out.println("登陆成功");
break;
}else{
count--;
System.out.println("密码错误"+count+"次机会");
}
}
}
}
运行结果: