将代码行从 C++ 转换为 JAVA

我想出了下面的代码。设法解决了大多数错误,除了与地图相关的错误。


我知道下面的代码行属于 C++。几天以来尝试了很多将其转换为JAVA,但无法找到方法:


下面是 C++ 中的代码行


map<Character,Integer> enc = new map<Character,Integer>();

注意:将上述语法改为HashMap/Map并导入Java.Util后,以下代码中标有3星的代码行显示以下错误“表达式的类型必须是数组类型,但它解析为Map”


1) enc[input.charAt(i)] = i; 2) int pos = enc[msg.charAt(i) - 32]; 3) int pos = enc[msg.charAt(i)];


// 此函数将破译任何输入消息


public static String ABC(String msg, String input)

        {

            // Hold the position of every character (A-Z) from encoded string 

            map<Character,Integer> enc = new map<Character,Integer>();

            for (int i = 0; i < input.length(); i++)

            {

                ***enc[input.charAt(i)] = i;***

            }


            String decipher = "";


            // This loop deciphered the message. 

            // Spaces, special characters and numbers remain same. 

            for (int i = 0; i < msg.length(); i++)

            {

                if (msg.charAt(i) >= 'a' && msg.charAt(i) <= 'z')

                {

                    ***int pos = enc[msg.charAt(i) - 32];***

                    decipher += plaintext.charAt(pos);

                }

                else if (msg.charAt(i) >= 'A' && msg.charAt(i) <= 'Z')

                {

                    ***int pos = enc[msg.charAt(i)];***

                    decipher += plaintext.charAt(pos);

                }

                else

                {

                    decipher += msg.charAt(i);

                }

            }

            return decipher;

        }


元芳怎么了
浏览 772回答 2
2回答

小唯快跑啊

map<Character,Integer> enc = new map<Character,Integer>();Map是 Java 中的一种接口类型,不能被实例化[作为匿名内部类以外的任何东西]。您需要实例化实现的类型之一Map,例如TreeMapor HashMap。//Since Java 7, <> infers the type argumentsMap<Character, Integer> enc = new HashMap<>();enc[input.charAt(i)] = i;您使用的括号运算符语法 ( enc[input.charAt(i)]) 是 C++ 原生的,在 Java 中不可重载;因此,Java 中唯一允许使用此类括号的情况是在使用数组时。您需要使用get()和put()配合 java 地图。enc.put(input.charAt(i), i);//...int pos = enc.get(msg.charAt(i) - 32);

米琪卡哇伊

老问题,但供将来参考,这里是解决方案:String plaintext = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";public static String ABC(String msg, String input)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // Hold the position of every character (A-Z) from encoded string&nbsp; &nbsp; &nbsp; &nbsp; Map<Character, Integer> enc = new HashMap<>();&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < input.length(); i++)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; des.put(input.charAt(i), i);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; String decipher = "";&nbsp; &nbsp; &nbsp; &nbsp; // This loop deciphered the message.&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // Spaces, special characters and numbers remain same.&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < msg.length(); i++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (msg.charAt(i) >= 'a' && msg.charAt(i) <= 'z')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int pos = enc.get((char)(msg.charAt(i)-32));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; decipher += plaintext.charAt(pos);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (msg.charAt(i) >= 'A' && msg.charAt(i) <= 'Z')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int pos = enc.get(mensaje.charAt(i));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; decipher += plaintext.charAt(pos);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; decipher += msg.charAt(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return decipher;&nbsp; &nbsp; }基本上你必须put在映射时使用,然后get在使用时使用它。哦,从 char 中减去它。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java