用于android的凯撒密码应用程序

时间:2015-04-14 16:53:17

标签: android

我的问题是如何将“空间”编码为编码文本中的空白区域,因为现在如果我输入文本例如abc'space'abc,我会得到abc的编码字母而不是空格我得到#但我想得到一个空的空间...而且对于解码,反向功能,如果我按空格我得到7但我想要一个空的空间..我不想编码空间或解码他的那个所有的想法

public class MainActivity extends ActionBarActivity {

    TextView myText, myText2;
    Button myCodeButton, myDecodeButton, deleteButton;
    public static EditText enteredEditText;
    public String getText;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myText = (TextView) findViewById(R.id.textView1);
        myText2 = (TextView) findViewById(R.id.textView2);
        enteredEditText = (EditText) findViewById(R.id.editText1);
        myCodeButton = (Button) findViewById(R.id.button1);
        myDecodeButton = (Button) findViewById(R.id.button2);
        deleteButton = (Button) findViewById(R.id.button3);


        Code_My_TextButton();
        Decode_my_textButton();

        // this is just for clearing edit_texts and text_views
        deleteClick();
    }

    public void Code_My_TextButton()
    {
        myCodeButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {

                Caesar_cipher_coding_method();
                myText2.setText("");
            }
        });
    }

    private void Caesar_cipher_coding_method() {


        int shift = 3;
        Editable msg = enteredEditText.getText();
        String s = "";
        int len = msg.length();
        for (int x = 0; x < len; x++) {
            char c = (char) (msg.charAt(x) + shift);
            if (c > 'z' || (c > 'Z' && c < 'd'))
            {
                c -= 26;
            }

            s += c;
        }
        myText.setText(s);
    }


    public void Decode_my_textButton()
    {
        myDecodeButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                Reverse_Caesar_cipher_coding_method();

            }
        });
    }

    private void Reverse_Caesar_cipher_coding_method() {
        int shift = -3;
        Editable msg = enteredEditText.getText();
        String s = "";
        int len = msg.length();
        for (int x = 0; x < len; x++) {
            char c = (char) (msg.charAt(x) + shift);
            if (c < 'A' || (c < 'a' && c > 'W'))
                c += 26;
            s += c;


        }
        myText2.setText(s);
        myText.setText("");
    }

    // this is just for clearing edit_texts and text_views
    public void deleteClick()
    {
        deleteButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                enteredEditText.setText("");
                myText.setText("");
                myText2.setText("");
            }
        });
    }

}

1 个答案:

答案 0 :(得分:1)

您需要做的就是在两个循环中检查它,如下所示:

for (int x = 0; x < len; x++) {
    if (Character.isWhitespace(msg.charAt(x))) {
        s += " ";
        continue;
    }
    char c = (char) (msg.charAt(x) + 3);
    if (c > 'z' || (c > 'Z' && c < 'd')) {
        c -= 26;
    }

    s += c;
}