如何创建编码器?解码器?

时间:2014-09-11 06:05:44

标签: java eclipse algorithm encoding decoding

我对编程很新,我想知道如何编码/解码输入的文本。

所有字母必须放下3个字母以表示前A - > d B - > Ë 等等

我输入一些伪代码作为例子:

  

输入消息:" LORYHBRX"

     

编码消息:LORYHBRX

     

解码消息:ILOVEYOU

     

输出信息:" ILOVEYOU"

请帮忙。

到目前为止我已经

import java.util.*;

public class Encoder {

public static void main(String[] args) 
{   

    String a = "d";
    String b = "e";
    String c = "f";
    String d = "g";
    String e = "h";
    String f = "i";
    String g = "j";
    String h = "k";
    String i = "l";
    String j = "m";
    String k = "n";
    String l = "o";
    String m = "p";
    String n = "q";
    String o = "r";
    String p = "s";
    String q = "t";
    String r = "u";
    String s = "v";
    String t = "w";
    String u = "x";
    String v = "y";
    String w = "z";
    String x = "a";
    String y = "b";
    String z = "c";

    Scanner in = new Scanner(System.in);
    System.out.println("Please enter the text you wish to encode.");
    String place = in.nextLine();
    System.out.println(place);

}
}

我试图将输入的内容转换为上述变量。

我认为我将所有这些字符串说明为变量,但我不知道如何通过输入使它们可以更改。

2 个答案:

答案 0 :(得分:1)

我会给你一些提示,如何进行解码和编码将只是相反的过程。

首先,您应该知道计算机只能理解调用Ascii code的数字。

Ascii代码b&A等字符的数字表示形式,因此,大写字母和小写字母为{{ 1}}这是int类型。

Read and See Ascci table here

您应该知道的另一个主题是Ascii code

强制转换正在将类型转换为另一种类型,例如将int转换为char或反之亦然,但是您应该知道某些类型的转换无法转换为类型boolean to int这样的另一种类型,这是不可能的。< / p>

Read about casting in Java

让我们谈谈编码并让解码给你解决,因为它与编码相反。

有关编码方式的提示

  1. 无需像Casting;
  2. 那样定义字符串
  3. 由于你了解了作为数字的Ascci代码,你可以通过转换来循环到97到122以获得小字符。

    示例:

    String a = "a"

    输出:

    System.out.println("Asci code of small a is " + (int)'a' + "\nsmall a is " + (char)97);
    
  4. 注意:您将char转换为int并将int转换为char。

    1. 由于您正在处理数字,因此您可以进行添加。

      示例:

      Asci code of small a is 97
      small a is a
      

      输出继电器:

      System.out.println("Three char after a is " +  (char)(97+3));
      
    2. 对于char x,y和z,你可以从他们的asci代码中减去23,例如there char after a is d 将给a的asci代码。

答案 1 :(得分:0)

完整的编码和解码代码

import java.util.Scanner;

public class coder {

    public static void main(String[] args) {
        String[] qe = {"1 ","2 ","3 ","4 ","5 ","6 ","7 ","8 ","9 ","10 ","11 ","12 ","13 ",
            "14 ","15 ","16 ","17 ","18 ","19 ","20 ","21 ","22 ","23 ","24 ","25 ","26 "};
        String [] eq = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o",
                "p","q","r","s","t","u","v","w","x","y","z"};
        Scanner in = new Scanner(System.in);
        System.out.println("Please enter the text you wish to encode.");
        String place = in.nextLine();
        place = place.toLowerCase();
        place = place.replace("a", qe[0]);
        place = place.replace("b", qe[1]);
        place = place.replace("c", qe[2]);
        place = place.replace("d", qe[3]);
        place = place.replace("e", qe[4]);
        place = place.replace("f", qe[5]);
        place = place.replace("g", qe[6]);
        place = place.replace("h", qe[7]);
        place = place.replace("i", qe[8]);
        place = place.replace("j", qe[9]);
        place = place.replace("k", qe[10]);
        place = place.replace("l", qe[11]);
        place = place.replace("m", qe[12]);
        place = place.replace("n", qe[13]);
        place = place.replace("o", qe[14]);
        place = place.replace("p", qe[15]);
        place = place.replace("q", qe[16]);
        place = place.replace("r", qe[17]);
        place = place.replace("s", qe[18]);
        place = place.replace("t", qe[19]);
        place = place.replace("u", qe[20]);
        place = place.replace("v", qe[21]);
        place = place.replace("w", qe[22]);
        place = place.replace("x", qe[23]);
        place = place.replace("y", qe[24]);
        place = place.replace("z", qe[25]);
        System.out.println(place);

        place = place.replace("26 ", eq[25]);
        place = place.replace("25 ", eq[24]);
        place = place.replace("24 ", eq[23]);
        place = place.replace("23 ", eq[22]);
        place = place.replace("22 ", eq[21]);
        place = place.replace("21 ", eq[20]);
        place = place.replace("20 ", eq[19]);
        place = place.replace("19 ", eq[18]);
        place = place.replace("18 ", eq[17]);
        place = place.replace("17 ", eq[16]);
        place = place.replace("16 ", eq[15]);
        place = place.replace("15 ", eq[14]);
        place = place.replace("14 ", eq[13]);
        place = place.replace("13 ", eq[12]);
        place = place.replace("12 ", eq[11]);
        place = place.replace("11 ", eq[10]);
        place = place.replace("10 ", eq[9]);
        place = place.replace("9 ", eq[8]);
        place = place.replace("8 ", eq[7]);
        place = place.replace("7 ", eq[6]);
        place = place.replace("6 ", eq[5]);
        place = place.replace("5 ", eq[4]);
        place = place.replace("4 ", eq[3]);
        place = place.replace("3 ", eq[2]);
        place = place.replace("2 ", eq[1]);
        place = place.replace("1 ", eq[0]);
        System.out.println(place);
    }

}