Java中的摩尔斯电码转换器

时间:2014-05-10 08:39:49

标签: java

public static void main(String[] args) {
    String A;
    Scanner bola = new Scanner(System.in);
    System.out.println("Message: ");
    A = bola.nextLine();
    int B = A.length();
    int C, X;
    Y = Y;
    X = 0;
    System.out.println("Translation: ");
    String Mor[] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-..", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};
    A = A.toLowerCase();
    for (C = 0; C < B; C++) {
        switch (A.substring(X, Y)) {
            case "a":
                System.out.print(Mor[0] + "|");
                X += X;
                Y += Y;
                break;
            case "b":
                System.out.print(Mor[1] + "|");
                X += X;
                Y += Y;
                break;
            case "c":
                System.out.print(Mor[2] + "|");
                X += X;
                Y += Y;
                break;
            case "d":
                System.out.print(Mor[3] + "|");
                X += X;
                Y += Y;
                break;
            case "e":
                System.out.print(Mor[4] + "|");
                X += X;
                Y += Y;
                break;
            case "f":
                System.out.print(Mor[5] + "|");
                X += X;
                Y += Y;
                break;
            case "g":
                System.out.print(Mor[6] + "|");
                X += X;
                Y += Y;
                break;
            case "h":
                System.out.print(Mor[7] + "|");
                X += X;
                Y += Y;
                break;
            case "i":
                System.out.print(Mor[8] + "|");
                X += X;
                Y += Y;
                break;
            case "j":
                System.out.print(Mor[9] + "|");
                X += X;
                Y += Y;
                break;
            case "k":
                System.out.print(Mor[10] + "|");
                X += X;
                Y += Y;
                break;
            case "l":
                System.out.print(Mor[11] + "|");
                X += X;
                Y += Y;
                break;
            case "m":
                System.out.print(Mor[12] + "|");
                X += X;
                Y += Y;
                break;
            case "n":
                System.out.print(Mor[13] + "|");
                X += X;
                Y += Y;
                break;
            case "o":
                System.out.print(Mor[14] + "|");
                X += X;
                Y += Y;
                break;
            case "p":
                System.out.print(Mor[15] + "|");
                X += X;
                Y += Y;
                break;
            case "q":
                System.out.print(Mor[16] + "|");
                X += X;
                Y += Y;
                break;
            case "r":
                System.out.print(Mor[17] + "|");
                X += X;
                Y += Y;
                break;
            case "s":
                System.out.print(Mor[18] + "|");
                X += X;
                Y += Y;
                break;
            case "t":
                System.out.print(Mor[19] + "|");
                X += X;
                Y += Y;
                break;
            case "u":
                System.out.print(Mor[20] + "|");
                X += X;
                Y += Y;
                break;
            case "v":
                System.out.print(Mor[21] + "|");
                X += X;
                Y += Y;
                break;
            case "w":
                System.out.print(Mor[22] + "|");
                X += X;
                Y += Y;
                break;
            case "x":
                System.out.print(Mor[23] + "|");
                X += X;
                Y += Y;
                break;
            case "y":
                System.out.print(Mor[24] + "|");
                X += X;
                break;
            case "z":
                System.out.print(Mor[25] + "|");
                X += X;
                Y += Y;
                break;
            default:
                System.out.print("|");
                break;

        }
    }
}

2 个答案:

答案 0 :(得分:1)

你的循环不正确它永远不会被执行。条件应为C <乙

答案 1 :(得分:0)

有一些初学者的错误

要增加1,请使用X += 1;或更短++X;。 你忘记了一次Y+=...。 C,X,Y(== X + 1)是两个冗余的变量。

for (int i = 0; i < A.length(); ++i) {
    char ch = A.charAt(i);
    ch = Character.toLowerCase(ch);
    if ('a' <= ch && ch <= 'z') {
       int j = ch - 'a';
       System.out.print(Mor[j]);
    }
    System.out.print("|");
}
System.out.println();

这使用带有计数器的for循环;在这里适当的。 样式:声明尽可能接近java中的用法。变量和函数名称以小写字母开头,遵循惯例。

由于Java在内部对文本使用Unicode(因此所有字符都可以混合并在java中使用),因此可以使用“&#39; a&#39;直到&#39; z&#39;按顺序编号。这里的类型从char扩展到int,并通过减去&#39; a&#39; (97),一个人获得了Mor的索引。