开发者社区 问答 正文

JAVA--我的代码运行前没有报错,可是就是运行不下去了,这是什么情况??大佬求解答? ?报错


这是什么情况啊???
我的代码如下:


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();
        }

    }
}

而且没有报错,只是在运行的时候才会跳出来

展开
收起
爱吃鱼的程序员 2020-06-23 01:32:00 625 分享 版权
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    判断逻辑有问题。我猜你是想要这种效果:

    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循环没有跳出语句

    2020-06-23 01:32:17
    赞同 展开评论