英语到莫尔斯电码翻译

时间:2014-05-10 09:05:58

标签: java

该代码假设从英语翻译为摩尔斯电码。一切正常,除了在for循环中,它只翻译Message(String A)的第一个字母。

为什么?

例如,我创建字符串A =" America" ...然后它变为"美国"(因为toLowerCase())但是当它进入for循环时,它只会翻译第一个" a"以及它,就像这样: " .- |||||||"

(a= ".-")..

我的代码:

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

        }
    }
}
}

3 个答案:

答案 0 :(得分:4)

我不知道你写的是什么而不是Y=Y;,但这没有任何影响(我不知道Y在哪里宣布)。您不需要使用子字符串 - 只需使用输入字符串的字符。

注意:如果您使用'a' - 'a' = 0'b' - 'a' = 1等属性,您的代码可能会更短(因此A. charAt(C) - &#39; a&#39;)将帮助你摆脱开关。

答案 1 :(得分:0)

问题出在这一行:

switch(A.substring(X,Y)){

var X从零开始:

X=0;

然后您将X增加X自己,这将使X等于零,因为0+0=0

X+=X;

你应该以某种方式使用var C作为索引迭代所有字符。在C的通话中使用A.substring(...)

答案 2 :(得分:0)

如果你需要,可以使用莫尔斯代码翻译器。

import java.util.Scanner;

公共课测试{

/**
 * @param args
 */
public static void main(String[] args) {
    System.out.println("Enter Text For Conversion to Morse Code");

    Scanner scan = new Scanner(System.in);
    String s = scan.nextLine();
    System.out.println(print(toMorse(s)));
}

static boolean[] toMorse(String input) {
    boolean output[] = new boolean[0];
    for (int i = 0; i < input.length(); i++) { // Every character
        char onChar = input.charAt(i);
        output = addCharacter(output, onChar);
    }
    return output;

}

static boolean[] addCharacter(boolean[] input, char character) {
    character = Character.toLowerCase(character);
    if (character == 'a') {
        input = dot(input);
        input = dash(input);
    }
    if (character == 'b') {
        input = dash(input);
        input = dot(input);
        input = dot(input);
        input = dot(input);
    }
    if (character == 'c') {
        input = dash(input);
        input = dot(input);
        input = dash(input);
        input = dot(input);
    }
    if (character == 'd') {
        input = dash(input);
        input = dot(input);
        input = dot(input);
    }
    if (character == 'e') {
        input = dot(input);
    }
    if (character == 'f') {
        input = dot(input);
        input = dot(input);
        input = dash(input);
        input = dot(input);
    }
    if (character == 'g') {
        input = dash(input);
        input = dash(input);
        input = dot(input);
    }
    if (character == 'h') {
        input = dot(input);
        input = dot(input);
        input = dot(input);
        input = dot(input);
    }
    if (character == 'i') {
        input = dot(input);
        input = dot(input);
    }
    if (character == 'j') {
        input = dot(input);
        input = dash(input);
        input = dash(input);
        input = dash(input);
    }
    if (character == 'k') {
        input = dash(input);
        input = dot(input);
        input = dash(input);
    }
    if (character == 'l') {
        input = dot(input);
        input = dash(input);
        input = dot(input);
        input = dot(input);
    }
    if (character == 'm') {
        input = dash(input);
        input = dash(input);
    }
    if (character == 'n') {
        input = dash(input);
        input = dot(input);
    }
    if (character == 'o') {
        input = dash(input);
        input = dash(input);
        input = dash(input);
    }
    if (character == 'p') {
        input = dot(input);
        input = dash(input);
        input = dash(input);
        input = dot(input);
    }
    if (character == 'q') {
        input = dash(input);
        input = dash(input);
        input = dot(input);
        input = dash(input);
    }
    if (character == 'r') {
        input = dot(input);
        input = dash(input);
        input = dot(input);
    }
    if (character == 's') {
        input = dot(input);
        input = dot(input);
        input = dot(input);
    }
    if (character == 't') {
        input = dash(input);
    }
    if (character == 'u') {
        input = dot(input);
        input = dot(input);
        input = dash(input);
    }
    if (character == 'v') {
        input = dot(input);
        input = dot(input);
        input = dot(input);
        input = dash(input);
    }
    if (character == 'w') {
        input = dot(input);
        input = dash(input);
        input = dash(input);
    }
    if (character == 'x') {
        input = dash(input);
        input = dot(input);
        input = dot(input);
        input = dash(input);
    }
    if (character == 'y') {
        input = dash(input);
        input = dot(input);
        input = dash(input);
        input = dash(input);

    }
    if (character == 'z') {
        input = dash(input);
        input = dash(input);
        input = dot(input);
        input = dot(input);
    }
    if (character == ' ') {
        input = add(input, false);
        input = add(input, false);
        input = add(input, false);
        input = add(input, false);
    }
    if (character == '1') {
        input = dot(input);
        input = dash(input);
        input = dash(input);
        input = dash(input);
        input = dash(input);

    }
    if (character == '2') {
        input = dot(input);
        input = dot(input);
        input = dash(input);
        input = dash(input);
        input = dash(input);

    }
    if (character == '3') {
        input = dot(input);
        input = dot(input);
        input = dot(input);
        input = dash(input);
        input = dash(input);

    }
    if (character == '4') {
        input = dot(input);
        input = dot(input);
        input = dot(input);
        input = dot(input);
        input = dash(input);

    }
    if (character == '5') {
        input = dot(input);
        input = dot(input);
        input = dot(input);
        input = dot(input);
        input = dot(input);

    }
    if (character == '6') {
        input = dash(input);
        input = dot(input);
        input = dot(input);
        input = dot(input);
        input = dot(input);

    }
    if (character == '7') {
        input = dash(input);
        input = dash(input);
        input = dot(input);
        input = dot(input);
        input = dot(input);

    }
    if (character == '8') {
        input = dash(input);
        input = dash(input);
        input = dash(input);
        input = dot(input);
        input = dot(input);

    }
    if (character == '9') {
        input = dash(input);
        input = dash(input);
        input = dash(input);
        input = dash(input);
        input = dot(input);

    }
    if (character == '0') {
        input = dash(input);
        input = dash(input);
        input = dash(input);
        input = dash(input);
        input = dash(input);
    }

    return input;
}

static boolean[] dot(boolean[] input) {
    input = add(input, true);
    input = add(input, false);
    return input;
}

static boolean[] dash(boolean[] input) {
    input = add(input, true);
    input = add(input, true);
    input = add(input, true);
    input = add(input, false);
    return input;
}

static boolean[] add(boolean[] input, boolean add) {
    int length = input.length;

    boolean[] newArray = new boolean[length + 1];
    for (int i = 0; i < length; i++) {
        newArray[i] = input[i];

    }
    newArray[length] = add;

    return newArray;

}

static String print(boolean[] input) {
    String output = "";
    for (int i = 0; i < input.length; i++) {
        if (input[i]) {
            output = output + "-";
        }
        if (!input[i]) {
            output = output + " ";
        }

    }

    return output;
}

}