第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

將代碼行從 C++ 轉(zhuǎn)換為 JAVA

將代碼行從 C++ 轉(zhuǎn)換為 JAVA

元芳怎么了 2022-01-19 15:46:39
我想出了下面的代碼。設(shè)法解決了大多數(shù)錯誤,除了與地圖相關(guān)的錯誤。我知道下面的代碼行屬于 C++。幾天以來嘗試了很多將其轉(zhuǎn)換為JAVA,但無法找到方法:下面是 C++ 中的代碼行map<Character,Integer> enc = new map<Character,Integer>();注意:將上述語法改為HashMap/Map并導(dǎo)入Java.Util后,以下代碼中標(biāo)有3星的代碼行顯示以下錯誤“表達(dá)式的類型必須是數(shù)組類型,但它解析為Map”1) enc[input.charAt(i)] = i; 2) int pos = enc[msg.charAt(i) - 32]; 3) int pos = enc[msg.charAt(i)];// 此函數(shù)將破譯任何輸入消息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貢獻(xiàn)1863條經(jīng)驗 獲得超2個贊

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

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


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

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

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

您使用的括號運(yùn)算符語法 ( enc[input.charAt(i)]) 是 C++ 原生的,在 Java 中不可重載;因此,Java 中唯一允許使用此類括號的情況是在使用數(shù)組時。


您需要使用get()和put()配合 java 地圖。


enc.put(input.charAt(i), i);

//...

int pos = enc.get(msg.charAt(i) - 32);


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

TA貢獻(xiàn)1998條經(jīng)驗 獲得超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 中減去它。


查看完整回答
反對 回復(fù) 2022-01-19
  • 2 回答
  • 0 關(guān)注
  • 662 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號