繁体   English   中英

需要帮助将已存储有元素的数组作为方法的参数传递给数组(JAVA)

[英]Need help passing an array with elements already stored into it as a parameter for a method (JAVA)

所以在这里,我有一段代码,它使用一个名为key(“ ALICE”)的字符串,并将其传递到keyReader()方法中,以获取每个索引的字母的位置-阐明A1L12I将是9C将是3E将是5 这些数字存储在名为keyArray[]的数组中。

我的问题是现在将keyArray[]与存储在其中的这5个元素一起使用,并将其作为参数传递到方法keyNumber()中,以便将每个数字更改为以27为基数,然后将其添加到总计为keyNo的情况(在这种情况下)。

任何帮助和建议,不胜感激。

public class Problem2 {

    public static void main(String[] args) {

        String key = "ALICE"; //VARIABLE - Will be read from text file
        String cipherThis = "JLMRSULTQTXLRCQQEBCHQFWWE"; //VARIABLE - Will be read from text file

        int noKey = 0;
        int[] keyArray = new int[5];

        keyReader(key, keyArray); //reads the key
        keyNumber(noKey, keyArray); //evaluates the keyNumber of keyReader

    }

    //Method for reading each letter of the key
    private static void keyReader(String key, int[] keyArray) {
        for (int x = 0; x < 5; x++) {
            keyArray[x] = key.charAt(x) - 64;
        }
    }

    //Method for evaluating the key number 
    private static void keyNumber(int noKey, int[] keyArray) {
        int i = 0; //Counter for the numbers of the letters stored into the array using the keyReader() method
        int k = 4; //Counter for the 5 letters of the key (4,3,2,1,0)

        while (i < 5) {
            while (k >= 0) {
                noKey += Math.pow(27, k) * keyArray[i];
                k--;
                i++;
            }
        }
    }

}

使用Integer(Object Reference)array而不是int array(原始)。 因此,您可以取回参考,以后可以将其用于进一步处理。

而不是使用...

private static void keyNumber(int noKey, int[] keyArray) {

尝试使用...

private static int keyNumber(int[] keyArray) {
    int noKey = 0;
    //...
    return noKey;
}

这将被称为使用...

noKey = keyNumber(keyArray); //evaluates the keyNumber of keyReader

您还应该考虑使用keyReader进行相同的keyReader ,而不是将其传递给数组,让它返回结果...

private static int[] keyReader(String key) {
    int[] keyArray = new int[key.length()];
    for (int x = 0; x < keyArray.length; x++) {
        keyArray[x] = key.charAt(x) - 64;
    }
    return keyArray;
}

并使用...

int[] keyArray = keyArray = keyReader(key); //reads the key

您不应该依赖幻数,而要按照已知的值工作...

代替...

int k = 4; //Counter for the 5 letters of the key (4,3,2,1,0)

while (i < 5) {
    while (k >= 0) {

您应该使用...

int k = keyArray.length - 1; //Counter for the 5 letters of the key (4,3,2,1,0)

while (i < keyArray.length) {
    while (k >= 0) {

代替...

在Java中,所有原语(如int)都按值传递(即复制)。 您要通过我的推荐信(即&)。 因此,使用新的Integer(someint)作为参数对int进行自动装箱(应该可行),否则最好从keyNumber函数中返回int(在添加之后)。

public static void main (String [] args){

    String key = "ALICE"; //VARIABLE - Will be read from text file
    String cipherThis = "JLMRSULTQTXLRCQQEBCHQFWWE"; //VARIABLE - Will be read from text file

    int noKey = 0;
    int[] keyArray = new int[key.lenght];

    keyReader(key, keyArray); //reads the key
    keyNumber (new Integer(noKey), keyArray); //evaluates the keyNumber of keyReader

}

//Method for reading each letter of the key
private static void keyReader(String key, int[] keyArray) {
    for (int x = 0; x < keyArray.length; x++){
        keyArray[x] = key.charAt(x)-64;
    }
}

//Method for evaluating the key number 
private static void keyNumber(Integer noKey, int[] keyArray){
    int i = 0; //Counter for the numbers of the letters stored into the array using the keyReader() method
    int k = 4; //Counter for the 5 letters of the key (4,3,2,1,0)

    while (i < 5){
        while (k >= 0){
            noKey += Math.pow(27, k)*keyArray[i];
            k--;
            i++;
        }
    }   
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM