0

作为对我初学者编程能力的挑战,我认为看看我是否可以编写一个简单的蛮力密码事情会很有趣。因此,我开始编写一个应用程序,该应用程序在给定字符串长度值的情况下生成它可能采用的每个字母数字排列。但是,由于我是一个完整的编程新手,所以我遇到了麻烦。

首先,尽管导入了 java.lang.Math,但我收到错误消息,提示找不到符号:pow。我设法通过写出完整的 java.lang.Math.pow(); 来解决这个问题;当我改用该功能时,但为什么它有效但导入不起作用却超出了我的范围。

其次,不管输入的长度如何,输入后我得到运行时错误:

aaException in thread "main" java.lang.ArrayIndexOutOfBoundsException: 98
at combination.main(combination.java:53)

这表明在第 53 行:

current[j] = alphanum[((int)current[j])+1];

我显然试图在 current[] 或 alphanum[] 中访问索引 98?据我所知,这不应该发生......

我对这种发展感到相当困惑。无论如何,这是我的代码:

//48-57 65-90 97-122

import java.util.Scanner;
import java.lang.Math;

public class combination {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        //Alphanum will be an array of chars: the lowercase letters of the alphabet, the uppercase, and the numbers 0-9.
        char[] alphanum = new char[62];

        //Set indexes 0-25 as lowercase a-z, and indexes 26-51 as uppercase A-Z, using ascii conversion.
        for (int i=0; i<26; i++) {
            alphanum[i] = (char)(i+97);
            alphanum[i+26] = (char)(i+65);
        }

        //Set indexes 51-61 as 0-9.
        for (int i=0; i<10; i++) {
            alphanum[i+52] = (char)(i+48);
        }

        //Take in variable for length.
        System.out.print("Enter length: ");
        int length = in.nextInt();

        //Current will be an array of chars: it will hold the current permutation being generated.
        char[] current = new char[length];

        //Set all indexes in current to "a" by default, and print this string as the first permutation.
        for (int i=0; i<length; i++) {
            current[i] = alphanum[0];
            System.out.print(current[i]);
        }

        //power will be a temporary double, used to calculate the number of iterations needed, as the pow function works with doubles.
        double power = (java.lang.Math.pow(62.00, ((double)length)));

        //Convert power to an integer, iterations, and subtract 1 because one iteration was already printed previously.
        int iterations = ((int)power)-1;

        /*The loop works like this. The rightmost char is checked, and if it is the maximum value of the idex
        it is reverted to idex 0 again and the index value of the char to the left of it is increased by 1,
        if it is not the maximum then it is just increased by 1. This is iterated the right number of times such
        that every alphanumeric permutation of that length has been returned.*/
        for (int i=0; i<iterations; i++) {
            for (int j=(length-1); j>=0; j--) {
                if ((j!=0) && (((int)current[j])==122)) {
                    current[j] = alphanum[0];
                    current[j-1] = alphanum[((int)current[j-1])+1];
                } else if (j!=0) {
                    current[j] = alphanum[((int)current[j])+1];
                } else {
                    System.out.println("This has looped too many times. Something is wrong.");
                }
            }

            //At the end of each iteration, print the string.
            for (int l=0; l<length; l++) {
                System.out.print(current[l]);
            }
        }
    }
}

我非常感谢您提供的任何帮助或见解。^_^

4

2 回答 2

4

您的alphanum数组大小为 62,其含义((int)current[j-1])+1为 98 (> 62)。

char 'a' 的 int 值为 97。

于 2011-12-10T19:33:50.340 回答
1

我显然试图在 current[] 或 alphanum[] 中访问索引 98?据我所知,这不应该发生......

这是完全可能的,因为您尝试在某个索引处访问 alphanum 中的元素,该索引是从当前数组的内容派生的。我建议你在不同的步骤打印出这些数组的内容,你会很快发现你的代码在哪里表现得不像你想象的那样

current[j] = alphanum[((int)current[j])+1];

在这里你尝试访问

int index = ((int)current[j])+1;
current[j] = alphanum[index];

索引似乎是 98

于 2011-12-10T19:32:37.153 回答