简体   繁体   English

如何将字符串输入与2D数组中的字符配对?

[英]How to pair string input with chars in a 2D array?

When I run the method below keep in mind ADFGVX will be printed to the left and over the top of the array when its displayed, just like a classic ADFGVX cypher. 当我运行下面的方法时,请记住,在显示时,ADFGVX将被打印到数组的左侧和顶部,就像经典的ADFGVX密码一样。

static char [][] poly = new char[][]{
        {'p','h','0','q','g','6'},
        {'4','m','e','a','1','y'},
        {'l','2','n','o','f','d'},
        {'x','k','r','3','c','v'},
        {'s','5','z','w','7','b'},
        {'j','9','u','t','i','8'}};

I have written a method that displays a polybius square using a 2d array(array can be seen above) and what I want to do is pair what ever the user enters with the square, so if the user types OBJECT I want it to return FG VX XA DF GV XG. 我已经编写了一种使用2d数组(上面可以看到数组)显示波利比乌斯广场的方法,而我想要做的就是将用户输入的东西与该广场配对,因此,如果用户键入OBJECT,我希望它返回FG VX XA DF GV XG。

Scanner console = new Scanner (System.in);

String phrase;

displayGrid();
System.out.println("");

System.out.print("Please enter a phrase you want to use\n");
phrase = console.nextLine();

console.close();

Does anyone here know how I would go about this? 这里有人知道我该怎么做吗? I was going to make a switch statement or something but I don't think that would work and even if it did it would be very long and inefficient. 我打算发表一个switch语句之类的东西,但是我认为那不会奏效,即使这样做也会很长且效率低下。

You could just iterate over your array to get the position of the character you are looking for and than decode this position to the letter. 您可以遍历数组以获取所需字符的位置,然后将此位置解码为字母。

public static String[] cypherADFGVX(String phrase){

    String[] output=new String[phrase.length()];

    for (int i = 0; i < phrase.length(); i++) {
        //optional for breaking
        //squareIteration:
        for (int j = 0; j < 6; j++) {
            for (int k = 0; k < 6; k++) {
                if(poly[j][k]==phrase.charAt(i)){
                    output[i]=new String(new char[]{switchChar(j),switchChar(k)});
                    //To stop the iteration over poly and take care of the next char
                    //break squareIteration;                    
                }
            }
        }
    }

    return output;
}

public static char switchChar(int integer){
    switch (integer) {
    case 0:     
        return 'A';
    case 1:
        return 'D';
    //and so on
    }
}

If I left any questions just ask. 如果我有任何疑问,请问。

To answer your questions 回答您的问题

Oh. 哦。 I see. 我懂了。 I made it a bit too complicated for java beginners. 我对Java初学者来说有点太复杂了。 An easier solution with just one String would be: 仅使用一个String的简单解决方案是:

public static String cypherADFGVX(String phrase){

    String output=new String[phrase.length()];

    for (int i = 0; i < phrase.length(); i++) {
        //optional for breaking
        //squareIteration:
        for (int j = 0; j < 6; j++) {
            for (int k = 0; k < 6; k++) {
                if(poly[j][k]==phrase.charAt(i)){
                    output=output+switchChar(j)+switchChar(k)+" ";
                    //To stop the iteration over poly and take care of the next char
                    //break squareIteration;                    
                }
            }
        }
    }

    return output;
}

Now let me explain what my lines do. 现在,让我解释一下我的台词。

String[] output=new String[phrase.length()];

creates a new array of string where each string are the two capital letters. 创建一个新的字符串数组,其中每个字符串都是两个大写字母。 It would look like ["FG","VX",...]. 看起来像[“ FG”,“ VX”,...]。 It is easier for futher processing in my opinion. 我认为,进行进一步处理比较容易。

 if(poly[j][k]==phrase.charAt(i))

Compares the character at position jk in your square with the i-th character of the input String. 将正方形中jk位置的字符与输入String的第i个字符进行比较。

output[i]=new String(new char[]{switchChar(j),switchChar(k)});

I use the String constructor that takes a char-array as argument. 我使用将char数组作为参数的String构造函数。

new char[]{'a','b'}

creates the array and fills it with the elements listed in the brackets. 创建数组,并用方括号中列出的元素填充数组。

Sure you can use the switch to set the value of a variable and than return that variable. 确保可以使用开关设置变量的值,然后返回该变量。

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

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