【问题标题】:testing a string to create a valid password测试字符串以创建有效密码
【发布时间】:2018-04-24 04:56:12
【问题描述】:

我正在创建一个 java 程序来测试密码的有效性。除了一些其他问题,我完全无法运行它。它编译得很好,但是当我尝试运行它时出现错误。

当我让它运行时,我的问题是我无法让我的代码正常工作。它需要读取用户输入字符串并使用代码规定的一些条件测试其有效性。它将一直运行,直到用户输入符合所有三个条件的有效密码。该代码在之前运行时会告诉错误条件已被破坏,或者在满足所有要求时会说密码无效。

我不太确定如何询问我的代码有什么问题,因为我不知道问题所在。

前提条件是我在我的代码中使用了几种方法。

到目前为止,这是我的代码:

import java.util.Scanner;
public class Test {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        
        // Prompt user to enter a password
        System.out.print("Enter your password: ");
        String x = input.nextLine();
        UserInput(x);
        
    }    
    
    public static String UserInput(String x) {
        
        // Define variables
        int l = x.length(),
                count = 0;
        
        char n = x.charAt(l);
        
        boolean valid = false,
                condition = false;
        
        String c1 = "", 
               s1 = "";
        
        
        // While loop tests the validity of the password
        while(valid == false && l <= 0){ 
            
            if (l < 8){
                condition = false;
                c1 = "A password must have at least eight characters";
                continue;
            }
            else if (Character.isLetter(n) == false && Character.isDigit(n) == false){
                condition = false;
                c1 = "A password must consist of only letters and digits";
                continue;
            }
            else
                condition = true;
            
            while (count < 2) {
                    if (Character.isDigit(n) == true) {
                        count++;
                    }
            }
            
            if (count < 2) {
                condition = false;
                c1 = "A password must contain at least two digits";
                continue;
            }
            
            else {
                condition = true;
            }
            
            l++;
            
            if (condition = true)
                valid = true;
            else
                valid = false;
        }
        
        if (valid == true)
            s1 = "Valid Passord";
        else
            s1 = "Invalid Password";
        
        String m = s1 + "\n\t" + c1;
        System.out.println(m);
        return x;
        
    }
}

【问题讨论】:

    标签: java debugging if-statement methods error-handling


    【解决方案1】:

    你设置密码长度-

    int l = x.length(),
    

    然后根据它循环。

        while(valid == false && l <= 0){ 
    

    “Loop while ...密码长度小于或等于零”是什么意思?

    另外,用于检查最小位数(请检查我的语法)-

    int ndx = 0;
    while (ndx < l) { 
      if ( Character.isDigit( x.charAt( ndx++ ) ) ) {
         count++;
      }
    }
    

    循环基于您检查的字符,而不是您期望找到的字符。

    【讨论】:

      【解决方案2】:
              while (count < 2) {
                      if (Character.isDigit(n) == true) {
                          count++;
                      }
              }
      

      在此循环中,您检查一个字符,如果字符串的最后一个字符不是数字,则循环可能会无限循环。

      【讨论】:

        猜你喜欢
        • 2010-10-16
        • 1970-01-01
        • 2019-06-29
        • 2016-01-15
        • 2017-01-17
        • 2021-06-09
        • 1970-01-01
        • 2010-10-01
        • 2011-01-13
        相关资源
        最近更新 更多