这是什么情况啊???
我的代码如下:
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();
}
}
}
而且没有报错,只是在运行的时候才会跳出来
判断逻辑有问题。我猜你是想要这种效果:
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
String password1 = null;
boolean flag = true;
int length = 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循环没有跳出语句