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

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

將七段轉換為數字

將七段轉換為數字

忽然笑 2021-10-21 15:06:07
我想在java中將七個段號轉換為普通字符串。例如,如果輸入這樣的字符串輸入   _  _     _  _  _  _  _  _    | _| _||_||_ |_   ||_||_|| | ||_  _|  | _||_|  ||_| _||_|輸出應該像1234567890我找到了這個 JavaScript答案,我正在嘗試將其轉換為 java。現在我有:private static void get7segment(String ascii)     {        String[] splited="909561432".split("");        HashMap<Integer,Integer> map=new HashMap<Integer,Integer>();        map.put(0, 63);        map.put(1, 6);        map.put(2, 91);        map.put(3, 79);        map.put(4, 102);        map.put(5, 109);        map.put(6, 125);        map.put(7, 7);        map.put(8, 127);        map.put(9, 111);                }任何幫助都會很高興
查看完整描述

2 回答

?
胡說叔叔

TA貢獻1804條經驗 獲得超8個贊

基于Nina Scholz 的想法:


public static void main(String[] args) {

String example= " _     _  _     _  _  _  _  _ \n| |  | _| _||_||_ |_   ||_||_| \n|_|  ||_  _|  | _||_|  ||_| _|";

        System.out.println(get7segment(example));

}


    private static String get7segment(String ascii) {

        String result = "";


        String[] lines = ascii.split("\n");

        String[] line1;

        String[] line2;

        String[] line3;


        for (int j = 0; j < lines.length - 2; j += 4) {

            line1 = lines[j].split("");

            line2 = lines[j + 1].split("");

            line3 = lines[j + 2].split("");


            String pow = "";

            int mod = 3;


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

                if (i % mod == 0) {

                    String strAs = digitToString(pow);


                    result += strAs;

                    pow = "";

                }


                if (line1[i].equals("_") && i % mod == 1)

                    pow += "0";


                if (line2[i].equals("|") && i % mod == 0)// left

                    pow += "5";

                if (line2[i].equals("|") && i % mod == 2)// right

                    pow += "1";

                if (line2[i].equals("_") && i % mod == 1)// bottom

                    pow += "6";


                if (line3[i].equals("|") && i % mod == 0)// left

                    pow += "4";

                if (line3[i].equals("|") && i % mod == 2)// right

                    pow += "2";

                if (line3[i].equals("_") && i % mod == 1)// bottom

                    pow += "3";


                if (line1.length - 1 == i) {

                    String strAs = digitToString(pow);

                    result += strAs;

                    pow = "";

                }

            }



            result += "\n";

        }

        return result;

    }


    /*

     * Converting single ascii digit to regular digit

     */

    private static String digitToString(String asciiDigit) {

        if (asciiDigit == null || asciiDigit.equals(""))

            return "";

        int pow = 0;

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

            pow += Math.pow(2, Character.getNumericValue(asciiDigit.charAt(i)));


        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>() {

            {

                put(63, 0);

                put(6, 1);

                put(91, 2);

                put(79, 3);

                put(102, 4);

                put(109, 5);

                put(125, 6);

                put(7, 7);

                put(127, 8);

                put(111, 9);

            }

        };

        return map.containsKey(pow) ? Integer.toString(map.get(pow)) : "?";

    }

輸出:


0123456789


查看完整回答
反對 回復 2021-10-21
?
暮色呼如

TA貢獻1853條經驗 獲得超9個贊

嘗試將三行字符串解析為數字序列將很棘手,因為在數字之間沒有某種分隔符。


123456789012345678901234567890

..._.._....._.._.._.._.._.._..

.|._|._||_||_.|_...||_||_||.|.

.||_.._|..|._||_|..||_|._||_|.

123456789012345678901234567890

上面是您的原始示例,其中的空格被點替換以突出顯示它們。請注意,有些數字是 1 個字符寬 ('1'),有些是 2 個字符 ('3', '7'),其余的則是 3 個字符。


問題: - '1's 總是只占兩列嗎?- '2' 和 '3' 之間以及 '6' 和 '7' 之間有一個空格,但其他數字之間沒有。在“1”之前還有一個前導空格。需要少于三列的字符是否會有前導空格的規則?- 還會有其他空間嗎?


我這樣做的方法是制作一個帶有三個字符串(三行)的對象。字符串必須等長。然后,實現一個 char-wise 解析器,將字符從三個字符串中平均提取出來。如果所有三個字符都是空格,請刪除它們。如果沒有,讀一欄,你有'1'嗎?如果沒有,請閱讀另一個,你有“3”還是“7”?如果沒有,請閱讀第三篇,你有什么性格?


這有幫助嗎?


查看完整回答
反對 回復 2021-10-21
  • 2 回答
  • 0 關注
  • 186 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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