Java大量增加字符串中的每个字符?

时间:2014-05-10 01:21:40

标签: java loops

目前我有一个要求输入字符串的程序,例如“Hello stackoverflow!”我试图通过一个大的随机数使每个字符增加。问题是,一旦程序到达'~'它必须开始倒退,然后一旦它'!'它必须再次切换。 (!是第一个Ascii符号,~是最后一个,中间有数字和字母。)到目前为止,这是我对增量部分所做的,虽然它不是很有用:

for(int i = 0; i < inputStringB.length(); i++){
    currentChar = inputString.charAt(i);

    for(int e = 0; e < 1000; e++){

        if(currentChar == '!'){
            switchIncrement = 1;
        } else if(currentChar == '~') {
            switchIncrement = 0;
        }

        switch(switchInc){
            case 0: currentChar--;
                inputStringB.setCharAt(i, currentChar);
                break;
            case 1: currentChar++;
                inputStringB.setCharAt(i, currentChar);
                break;
            default:  currentChar++;
                inputStringB.setCharAt(i, currentChar);
                break;
        }
    }               
    System.out.println(inputStringB);   
}

当我输入'Hello'时收到:'n#J&)'考虑到'Hello'包含两个l,这是没有意义的,所以我希望'n#JJ)'

任何帮助都会很棒,谢谢!

完整代码:

  import java.util.Scanner;
public class EncryptionMain {
    public static void main(String args[]){

        char currentChar;
        String EorD;
        int switchInc = 1; // 0 = BACK 1 = FORWARD 
        Scanner scanner = new Scanner(System.in);

        System.out.println("Encrypt or decrypt: ");

        EorD = scanner.next();

        if(EorD.equalsIgnoreCase("e")){

            System.out.println("Please enter text to encrypt: ");

            String inputString = scanner.next();

            System.out.println("Encrypting: '" + inputString + "' ");
            System.out.println("...");
            System.out.println("...");
            System.out.println("...");
            StringBuilder inputStringB = new StringBuilder(inputString);
            for(int i = 0; i < inputStringB.length(); i++){
                currentChar = inputStringB.charAt(i);
                System.out.println(currentChar);
                for(int e = 0; e < 1000; e++){

                        if(currentChar == '!'){
                            switchInc = 1;
                        }else if(currentChar == '~'){
                            switchInc = 0;
                        }

                        switch(switchInc){

                        case 0: currentChar--;
                                inputStringB.setCharAt(i, currentChar);
                                break;

                        default:  currentChar++;
                                  inputStringB.setCharAt(i, currentChar);
                                  break;
                        }
                }

                System.out.println(inputStringB);   
            }
        }else if(EorD.equalsIgnoreCase("d")){

            System.out.println("Please enter text to decrypt: ");

            String inputString = scanner.next();

            System.out.println("Decrypting: '" + inputString + "' ");
            System.out.println("...");
            System.out.println("...");
            System.out.println("...");
            StringBuilder inputStringB = new StringBuilder(inputString);
            for(int i = 0; i < inputStringB.length(); i++){
                currentChar = inputStringB.charAt(i);

                for(int e = 0; e < 1000; e++){
                    if(currentChar == '!'){
                            switchInc = 0;
                    }else if(currentChar == '~'){
                            switchInc = 1;
                    }
                        switch(switchInc){
                        case 0: currentChar++;
                                inputStringB.setCharAt(i, currentChar);
                                break;
                        case 1: currentChar--;
                                inputStringB.setCharAt(i, currentChar);
                                break;
                        default:  currentChar--;
                                  inputStringB.setCharAt(i, currentChar);
                                  break;
                        }
                }

                inputStringB.setCharAt(i, currentChar);
                System.out.println(inputStringB);   
            }
        }

    }
}

1 个答案:

答案 0 :(得分:1)

首先,我将删除所有I / O(输出结果除外),为简洁起见删除解密部分,并将encrypt(String)encrypt(char)写为{{3 }}

public class EncryptionMain {

    public static void main(String args[]){
        System.out.println(encrypt("Hello"));
    }

    static String encrypt(String plaintext) {
        StringBuilder ciphertext = new StringBuilder(plaintext);
        for (int i = 0; i < ciphertext.length(); i++) {
            ciphertext.setCharAt(i, encrypt(ciphertext.charAt(i)));
        }
        return ciphertext.toString();
    }

    static char encrypt(char c) {
        int switchInc = 1; // 0 = BACK 1 = FORWARD

        for(int e = 0; e < 1000; e++){
            if (c == '!') {
                switchInc = 1;
            } else if (c == '~') {
                switchInc = 0;
            }

            switch (switchInc) {
                case 0: c--; break;
                default: c++; break;
            }
        }
        return c;
    }
}

而且,哎呀! - 只是通过这样做,我不小心修复了这个bug。在这里,我将其添加回来:

public class EncryptionMain {

    public static void main(String args[]){
        System.out.println(encrypt("Hello"));
    }

    static String encrypt(String plaintext) {
        StringBuilder ciphertext = new StringBuilder(plaintext);
        for (int i = 0; i < ciphertext.length(); i++) {
            ciphertext.setCharAt(i, encrypt(ciphertext.charAt(i)));
        }
        return ciphertext.toString();
    }

    static int switchInc = 1; // 0 = BACK 1 = FORWARD

    static char encrypt(char c) {

        for(int e = 0; e < 1000; e++){
            if (c == '!') {
                switchInc = 1;
            } else if (c == '~') {
                switchInc = 0;
            }

            switch (switchInc) {
                case 0: c--; break;
                default: c++; break;
            }
        }
        return c;
    }
}

编辑 - 这是我在下面的评论中提到的一个有效的Caesar密码。

主/加密/ Cipher.java

package cipher;

public final class Cipher {

    public Cipher(Range range, int key) {
        this.range = range;
        this.key = key;
    }

    public final Range range;
    public final int key;

    public String encrypt(String plaintext) {
        return cipher(plaintext, key);
    }

    public String decrypt(String ciphertext) {
        return cipher(ciphertext, -key);
    }

    String cipher(String in, int n) {
        StringBuilder out = new StringBuilder(in.length());
        for (int i = 0; i < in.length(); i++) {
            out.append(range.shift(in.charAt(i), n));
        }
        return out.toString();
    }
}

主/加密/ Range.java

package cipher;

public final class Range {

    public final char min;
    public final char max;
    public final int size;

    public static Range inclusive(char min, char max) {
        return new Range(min, max);
    }

    Range(char min, char max) {
        this.min = min;
        this.max = max;
        size = max - min + 1;
    }

    /** Shift c up by i places, wrapping around to the
     * beginning of the range when it reaches the end. */
    public char shift(char c, int i) {
        return (char) (min + mod(c - min + i, size));
    }

    /** x mod a */
    static int mod(int x, int a) {
        return ((x % a) + a) % a;
    }
}

测试/加密/ CipherTest.java

package cipher;

import org.testng.annotations.Test;

import static org.testng.Assert.assertEquals;

public class CipherTest {

    @Test
    public void testZeroKey() throws Exception {
        Cipher cipher = new Cipher(Range.inclusive('a', 'z'), 0);
        assertEquals(cipher.encrypt("abcxyz"), "abcxyz");
        assertEquals(cipher.decrypt("abcxyz"), "abcxyz");
    }

    @Test
    public void testOneKey() throws Exception {
        Cipher cipher = new Cipher(Range.inclusive('a', 'z'), 1);
        assertEquals(cipher.encrypt("abcxyz"), "bcdyza");
        assertEquals(cipher.decrypt("bcdyza"), "abcxyz");
    }

    @Test
    public void testSizePlusOneKey() throws Exception {
        Cipher cipher = new Cipher(Range.inclusive('a', 'z'), 27);
        assertEquals(cipher.encrypt("abcxyz"), "bcdyza");
        assertEquals(cipher.decrypt("bcdyza"), "abcxyz");
    }
}

测试/加密/ RangeTest.java

package cipher;

import org.testng.Assert;
import org.testng.annotations.Test;

import static org.testng.Assert.assertEquals;
import static cipher.Range.mod;

public class RangeTest {

    @Test
    public void testSize() {
        Assert.assertEquals(Range.inclusive('a', 'c').size, 3);
    }

    @Test
    public void testMod() throws Exception {
        assertEquals(mod(-2, 5), 3);
        assertEquals(mod(-1, 5), 4);
        assertEquals(mod(0, 5), 0);
        assertEquals(mod(1, 5), 1);
        assertEquals(mod(2, 5), 2);
        assertEquals(mod(3, 5), 3);
        assertEquals(mod(4, 5), 4);
        assertEquals(mod(5, 5), 0);
        assertEquals(mod(6, 5), 1);
    }

    @Test
    public void testShift() throws Exception {
        Range r = Range.inclusive('a', 'd');
        Assert.assertEquals(r.shift('a', -2), 'c');
        Assert.assertEquals(r.shift('a', -1), 'd');
        Assert.assertEquals(r.shift('a', 0), 'a');
        Assert.assertEquals(r.shift('a', 1), 'b');
        Assert.assertEquals(r.shift('a', 2), 'c');
        Assert.assertEquals(r.shift('a', 3), 'd');
        Assert.assertEquals(r.shift('a', 4), 'a');
        Assert.assertEquals(r.shift('a', 5), 'b');
    }
}