【问题标题】:Converting seven segment to number [closed]将七段转换为数字[关闭]
【发布时间】:2019-09-18 16:44:35
【问题描述】:

我想在java中将七段数字转换为普通字符串。例如,如果输入这样的字符串

输入

   _  _     _  _  _  _  _  _   
 | _| _||_||_ |_   ||_||_|| |
 ||_  _|  | _||_|  ||_| _||_|

输出应该是这样的

1234567890

我找到了this 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);            
    }

任何帮助都会得到帮助

【问题讨论】:

  • 从问题中不清楚你到底卡在哪里了。
  • 对于 javascript,您可能会发现 this 很有趣...
  • @NinaScholz 你好尼娜,我已经在我的问题中提到了你的答案,我正在尝试将代码转换为 Java。因为 JS 不是我的强项
  • 对不起,tl;dr ;-)
  • @NinaScholz 我很乐意接受你的回答 :)

标签: javascript java seven-segment-display


【解决方案1】:

基于Nina Scholz's的想法:

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

【讨论】:

  • 很有趣,如果我的回答足够好,可以转换,为什么还不够好来投票?
  • 不能说。 java 不是我的一杯茶。
【解决方案2】:

如果没有数字之间的某种分隔符,尝试将一个三行字符串解析为一个数字序列将会很棘手。

123456789012345678901234567890
..._.._....._.._.._.._.._.._..
.|._|._||_||_.|_...||_||_||.|.
.||_.._|..|._||_|..||_|._||_|.
123456789012345678901234567890

以上是您的原始示例,其中空格替换为点以突出显示它们。 请注意,有些数字是 1 个字符宽('1'),有些是两个('3','7'),其余的是三个。

问题: - '1' 总是只占两列吗? - '2' 和 '3' 之间以及 '6' 和 '7' 之间有一个空格,但其他数字之间没有。在“1”之前还有一个前导空格。要求少于三列的字符是否有前导空格? - 还会有其他空间吗?

我这样做的方法是制作一个带有三个字符串(三行)的对象。字符串必须具有相同的长度。然后,实现一个 char-wise 解析器,从三个字符串中平均提取字符。如果所有三个字符都是空格,则删除它们。如果没有,请阅读一列,你有'1'吗?如果没有,请阅读另一个,你有“3”还是“7”?如果没有,请读第三个,你有什么性格?

这有帮助吗?

【讨论】:

  • 当然,可以使用引用的 javascript 答案中建议的模式技术来回答“你有一个特定的数字”这个问题。即:使用所有三行中的字符构造一个哈希值,并将其与每个字符的常量进行比较。
猜你喜欢
  • 2010-12-07
  • 1970-01-01
  • 2021-08-29
  • 2019-12-30
  • 2019-11-01
  • 1970-01-01
  • 2021-08-07
  • 1970-01-01
相关资源
最近更新 更多