【问题标题】:Validate number Input [closed]验证数字输入
【发布时间】:2017-03-25 11:19:52
【问题描述】:

我正在处理一个验证问题,我需要想出一个验证解决方案。

用户应该输入一个密码,这是一个 4 位数字。应用于它的验证规则应该是。

  1. 密码不能是连续数字,即1234或9876
  2. 不能重复,即1111、2222
  3. 不能是三个数的连续序列,即1345、9765
  4. 不能是同一个数字三元组,即 2444、1115。

现在,我到目前为止所做的是: 1.我尝试使用这个公式,它在另一个解决方案中给出,但它没有帮助。 将输入分解为 int[] 并从中获取第一个和最后一个项目,然后应用验证。

A[length-1] - A[0] = A.length-1 这适用于连续数字 1234、9876,但是当我输入有效的 1324 时它会中断。

然后,为了验证重复项,我想到了使用一个集合,输入集合中的数字,然后检查集合的大小,例如 1111、2222。但是,在 1212 的情况下它再次失败,这是一个有效的输入。

虽然,这可以通过从每个数字中加或减 1 来解决,并检查它是否连续,但我正在寻找其他的东西。该解决方案将是我的最后一种方法。

感谢java中的解决方案。如果没有,那么简单的伪代码也可以。 假设只能给出数字作为输入。

更新: 对不起,伙计们,我不把 SO 作为做功课的一种方式,但我认为这些信息就足够了。无论如何,这就是我到目前为止所管理的,

private boolean checkFirstOrderAscendingDescending(int[] array, int length) {
        // to handle passwords like 1234, 9876
        int arrayItemOne = array[0];
        int arrayItemLast = array[length - 1];

        if (Math.abs(arrayItemLast - arrayItemOne) == length - 1) {
            return false;
        } else {
            return true;
        }
    }

private boolean checkDuplicates(int[] array, int length) {
        // to handle passwords like 1112, 9998
        int arrayItemOne = array[0];
        int arrayItemTwo = array[1];
        int arrayItemThree = array[2];

        if (arrayItemOne == arrayItemTwo && arrayItemTwo == arrayItemThree) {
            return false;
        } else {
            return true;
        }
    }

check duplicates 方法可以扩展为检查数组的第 1、2、3 个索引中的重复项。在调用这些方法之前,会对数组进行空检查。还有就是保证数组的长度永远是4,所以请求特定的索引永远不会给ArrayIndexOutOfBounds。

我希望现在提供的信息已经足够了。

【问题讨论】:

  • 你能给我们看看你已经做过的代码,这样我们就不用你做所有的功课了吗?
  • 正如您的代表应该知道的那样,SO 不是编码服务。更糟糕的是,你要求社区做你的功课。请努力,发布您的代码并询问您遇到的更具体的问题。
  • 具有讽刺意味的是(和题外话),从暴力破解的角度来看,这些都使得实际的密码选择更弱。
  • @vibhor_shri 如果您的问题得到解答,请标记为这样。
  • @Japu_D_Cret,我无法测试它。我会尽快完成,并会更新。

标签: java android validation


【解决方案1】:

这是我的解决方案。请不要将 SO 视为绕过作业的一种方式

private static final int MAXIMUM_DUPLICATES = 2;
private static final int MAXIMUM_SERIES = 2;
private static final int SERIES_DIRECTION_ASCENDING = 0x42;
private static final int SERIES_DIRECTION_DESCENDING = ~SERIES_DIRECTION_ASCENDING;
public static boolean isPasswordValid(String pwd) {
    char lastChar = '\0';
    int duplicateCount = 1;
    int seriesCount = 1;
    int seriesDirection = 0; 

    char[] chars = pwd.toCharArray();
    for(char c : chars) {
        //skip the first character
        if(lastChar > 0) {
            if(lastChar == c)
                duplicateCount++;
            else
                duplicateCount = 1;

            if(lastChar == c+1) {
                //was the previous char of the same series? if not then reset the seriesCount
                if(seriesDirection != SERIES_DIRECTION_ASCENDING) {
                    seriesCount = 1;
                    seriesDirection = SERIES_DIRECTION_ASCENDING;
                }

                seriesCount++;
            } else if(lastChar == c-1) {
                if(seriesDirection != SERIES_DIRECTION_DESCENDING) {
                    seriesCount = 1;
                    seriesDirection = SERIES_DIRECTION_DESCENDING;
                }

                seriesCount++;
            } else {
                seriesCount = 1;
            }

            //are there more duplicates then are allowed?
            if(duplicateCount > MAXIMUM_DUPLICATES)
                return false;

            //is there a longer series then allowed?
            if(seriesCount > MAXIMUM_SERIES)
                return false;
        }

        //save the last character
        lastChar = c;
    }

    return true;
}

这是我的检查方法

public static void main(String[] args) {
    Map<String, Boolean> inputsAndExpectedResults = new HashMap<>();

    inputsAndExpectedResults.put("1234", false);
    inputsAndExpectedResults.put("4321", false);
    inputsAndExpectedResults.put("1122", true);
    inputsAndExpectedResults.put("8143", true);
    inputsAndExpectedResults.put("1265", true);
    inputsAndExpectedResults.put("9233", true);
    inputsAndExpectedResults.put("9137", true);
    inputsAndExpectedResults.put("2222", false);
    inputsAndExpectedResults.put("4333", false);
    inputsAndExpectedResults.put("5421", true);
    inputsAndExpectedResults.put("6667", false);
    inputsAndExpectedResults.put("4569", false);
    inputsAndExpectedResults.put("1272", true);

    Set<String> keySet = inputsAndExpectedResults.keySet();
    for(String key : keySet) {
        Boolean expectedValue = inputsAndExpectedResults.get(key);

        boolean result = isPasswordValid(key);
        String output = String.format("isPasswordValid(%s) returned %b expected %b", key, result, expectedValue);
        if(expectedValue.equals(result))
            System.out.println(output);
        else
            System.err.println(output);
    }
}

【讨论】:

  • 它确实有效,@Japu_D_Cret。感谢您的解决方案
猜你喜欢
  • 1970-01-01
  • 2023-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多