该怎么做才能在这个java代码中重试

时间:2017-01-20 03:31:41

标签: java

当我输入密码而不需要密码时,我不会让我重试密码来获取礼仪密码。我该如何解决。我不知道这是一个循环的东西或什么的。这是我的代码:

import java.util.Scanner;

public class Password {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    int min =8;
    int max=16;
    int digit=0;
    int special=0;
    int upCount=0;
    int loCount=0;
    String password;
    Scanner scan = new Scanner(System.in);
    System.out.println(" Enter Your Password:");
        password = scan.nextLine();
    if(password.length()>=min&&password.length()<=max){
        for(int i =0;i<password.length();i++){
            char c = password.charAt(i);
            if(Character.isUpperCase(c)){
                upCount++;
            }
            if(Character.isLowerCase(c)){
                loCount++;
            }
            if(Character.isDigit(c)){
                digit++;
            }
            if(c>=33&&c<=46||c==64){
                special++;
            }
        }
        if(special>=1&&loCount>=1&&upCount>=1&&digit>=1){
            System.out.println(" Password accepted!");
        }

    }

    if(password.length()<min){

        for(int i =0;i<password.length();i++){
            char c = password.charAt(i);
            if(Character.isLowerCase(c)){
                loCount++;
            }
            }

        if(loCount>0){
            System.out.println(" Password must be atleat "+min+" characters");
    }
    }
    else if(password.length()<min&&upCount>1){
        for(int i =0;i<password.length();i++){
        char c =password.charAt(i);
        if(Character.isLowerCase(c)){
            loCount++;
        }
         if(Character.isUpperCase(c)){
            upCount++;
        }
        }
        if(loCount>0&&upCount>0){
        System.out.println(" Password must be atleast "+min+" chracters:");
        System.out.println(" You need atleast one digit:");
        System.out.println(" You need atleast one special chracter:");
    }
    }
     if(password.length()>max||password.length()>=max&&upCount>1&&loCount>1&&digit>1){
         System.out.println(" Password is too long.Limit is "+max+" chracters:");
                 System.out.println(" You need atleast one special chracter:");

        }
      if(password.length()>=min&&password.length()<=max&&loCount>0&&upCount>0&&digit>0&&special==0){
         System.out.println(" You need atleast a special chracter");
     }
      if(password.length()>=min&&password.length()<=max&&loCount>0&&upCount>0&&digit==0&&special==0){
         System.out.println(" You need atleast one digit:");
         System.out.println(" You need atleast one special chracter:");
     }
   }
}

1 个答案:

答案 0 :(得分:0)

password = scan.nextLine();

您只添加了此行一次,因此它不会再次接受密码。 只要您的密码验证失败,您就必须再次调用它。其他一些提示:

您现有的所有if()应更新一个boolean字段和一个消息字段,例如isPasswordOkpasswordMessage。然后添加

System.out.println(passwordMessage);

if(!isPasswordOk){
password = scan.nextLine();
}