Java 如何在Polybius密码中打印字母的特定坐标?

Java 如何在Polybius密码中打印字母的特定坐标?,java,multidimensional-array,Java,Multidimensional Array,我试图打印出二维数组中字母的特定坐标。它是ADVGVX密码中的一个Polybius正方形,我只想打印出数组中的一个位置,例如“a”,即(1,3) 我试图通过使用for循环和if语句来实现这一点 public void printArrayElement(){ for(int row = 0; row < cypher.length; row++){ for(int column = 0; column < cyphe

我试图打印出二维数组中字母的特定坐标。它是ADVGVX密码中的一个Polybius正方形,我只想打印出数组中的一个位置,例如“a”,即(1,3)

我试图通过使用for循环和if语句来实现这一点

          public void printArrayElement(){
           for(int row = 0; row < cypher.length; row++){
             for(int column = 0; column < cypher [row].length; column++){
                if (cypher[row][column] == cypher [1][3]){
                    System.out.println(cypher[row][column]);
                  }
                }
              }
            }
public void printArrayElement(){
for(int row=0;row
我没有收到任何错误消息,什么都没有发生

编辑: 实际上,我无法将此作为主要方法运行。仅凭上述内容,我就得到了信息:

错误:在类中找不到Main方法。PolybiusCypher,请将Main方法定义为: 公共静态void main(字符串[]args) 或者JavaFX应用程序类必须扩展JavaFX.application.application

当我使用公共静态void main(String[]args)
时,会收到多条错误消息

public class PolybiusCypher {
    public char[][] cypher = {
        {'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', 'c', 'w', '7', 'b'}, 
        {'j', '9', 'u', 't', 'i', '8'},};

    public void printArrayElement(){
        System.out.println(cypher[1][3]);
    }

    void main(String[] args) {
        // Need to create instance of PolybiusCypher to access its fields (cypher)
        new PolybiusCypher().printArrayElement();
    }
}

或者,您可以将
cypher
printArrayElement
设置为静态,不需要创建Instance。

我认为在注释中包含我测试过的代码可能会有用。如果您将其放在一个名为polybiucipher.java的文件中,然后尝试编译并运行它,您应该会看到与预期一样的输出“a”

public class PolybiusCipher{

    public static char[][] cypher = {
            {'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', 'c', 'w', '7', 'b'},
            {'j', '9', 'u', 't', 'i', '8'},};

    public static void main(String[] args) {
        printArrayElement();
    }

    public static void printArrayElement(){
        for(int row = 0; row < cypher.length; row++){
            for(int column = 0; column < cypher [row].length; column++){
                if (cypher[row][column] == cypher [1][3]){
                    System.out.println(cypher[row][column]);
                }
            }
        }
    }
}
public-class PolybiusCipher{
公共静态字符[][]密码={
{'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','c','w','7','b'},
{'j','9','u','t','i','8'},};
公共静态void main(字符串[]args){
printArrayElement();
}
公共静态无效printArrayElement(){
for(int row=0;row

如果您想了解更多信息,本文提供了一些关于Java中主要方法的好信息。

为什么有for循环?只是
System.out.println(cypher[1][3])将足以打印出(1,3)。我运行了您的代码并得到了输出“a”。有没有可能你没有从主方法运行你的方法?@AbsoluteSpace嘿,非常感谢。实际上,我在将其作为主方法运行时遇到了问题,我收到了一些错误消息,我将在上面澄清。
voidmain(args)
是在java中根程序执行的唯一方法,您必须在某个地方找到它。“我收到多条错误消息”对我们没有多大帮助,我猜您应该尝试直接从
main
调用
printArrayElement
,或者用
main
替换
printArrayElement
。尝试
void main(String[]args){new PolybiusCypher().printarayelement();}
public class PolybiusCipher{

    public static char[][] cypher = {
            {'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', 'c', 'w', '7', 'b'},
            {'j', '9', 'u', 't', 'i', '8'},};

    public static void main(String[] args) {
        printArrayElement();
    }

    public static void printArrayElement(){
        for(int row = 0; row < cypher.length; row++){
            for(int column = 0; column < cypher [row].length; column++){
                if (cypher[row][column] == cypher [1][3]){
                    System.out.println(cypher[row][column]);
                }
            }
        }
    }
}