亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將代碼行從 C++ 轉換為 JAVA

將代碼行從 C++ 轉換為 JAVA

元芳怎么了 2022-01-19 15:46:39
我想出了下面的代碼。設法解決了大多數錯誤,除了與地圖相關的錯誤。我知道下面的代碼行屬于 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;        }
查看完整描述

2 回答

?
小唯快跑啊

TA貢獻1863條經驗 獲得超2個贊

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

Map是 Java 中的一種接口類型,不能被實例化[作為匿名內部類以外的任何東西]。您需要實例化實現的類型之一Map,例如TreeMapor HashMap。


//Since Java 7, <> infers the type arguments

Map<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);


查看完整回答
反對 回復 2022-01-19
?
米琪卡哇伊

TA貢獻1998條經驗 獲得超6個贊

老問題,但供將來參考,這里是解決方案:


String plaintext = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

public static String ABC(String msg, String input)

    {

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

        Map<Character, Integer> enc = new HashMap<>();

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

        {

        des.put(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.get((char)(msg.charAt(i)-32));

                decipher += plaintext.charAt(pos);

            }

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

            {

                int pos = enc.get(mensaje.charAt(i));

                decipher += plaintext.charAt(pos);

            }

            else

            {

                decipher += msg.charAt(i);

            }

        }

        return decipher;

    }

基本上你必須put在映射時使用,然后get在使用時使用它。哦,從 char 中減去它。


查看完整回答
反對 回復 2022-01-19
  • 2 回答
  • 0 關注
  • 702 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號