import java.util.Scanner;
public class 模拟QQ设置密码时检测长度 {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
String password1="这个是初值,随便写一个即可";
int length = password1.length();
while(true) {//当字符长度在6~18之间
System.out.println("请输入密码"+"(字符长度在6~18位之间)");//
password1=sc.nextLine();
if(length<6 || length>18){
System.out.println("密码不符合规范,请控制密码长度在6~18位之间");
}else {
System.out.println("恭喜您,密码设定成功");
}
sc.close();
}
}
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
判断逻辑有问题。我猜你是想要这种效果:
publicstaticvoidmain(String[]args)throwsException{Scannersc=newScanner(System.in);Stringpassword1=null;booleanflag=true;intlength=0;System.out.println("请输入密码"+"(字符长度在6~18位之间)");//do{password1=sc.nextLine();if(null!=password1&&!password1.isEmpty()){length=password1.length();}flag=(length<6||length>18);if(flag){System.err.println("密码长度不满足!(密码字符长度在6~18位之间)");}}while(flag);System.out.println("恭喜您,密码设定成功");System.out.println("设定密码为:"+password1);sc.close();}
sc.close();要放在while循环外面
while循环没有跳出语句