Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java将字符串中的每个字符大量递增?_Java_Loops - Fatal编程技术网

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

Java将字符串中的每个字符大量递增?,java,loops,Java,Loops,目前,我有一个程序要求输入一个字符串,例如“Hello stackoverflow!”,我试图让它以一个大的随机数递增每个字符。问题是,一旦程序到达“~”它就必须开始倒退,然后一旦它到达“!”它必须再次切换。( 是第一个ASCII符号, ~是最后一个,中间有数字和字母。到目前为止,这是增量部分的内容,但它不是很有效: for(int i = 0; i < inputStringB.length(); i++){ currentChar = inputString.charAt(i)

目前,我有一个程序要求输入一个字符串,例如“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);   
}

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

test/cipher/CipherTest.java

test/cipher/RangeTest.java


那么你做了什么来调试它呢?你的案例1是不需要的。默认值做同样的事情,如果没有显式的1大小写,它会捕捉到它?我看不出你给出的代码有任何随机性。@ChrisMartin我打算补充一点,一旦我解决了主要问题,我可能不应该这么说。这将有助于后期编译代码。所有这些变量都是未声明的:
inputStringB
inputString
currentChar
switchIncrement
switchInc
。哇,我通过问一个问题学到了很多东西,喜欢这个网站。但是你能告诉我我的代码出了什么问题吗?我还是说不出来。我注意到还有一件事,如果for循环中有一个很大的数字,“int e=0;e<1000…”程序就不能运行了。请仔细看看我答案中两个版本之间的差异。不同之处在于
switchInc
的范围。每次加密字符时,该变量都需要初始化为
1
。您不能共享变量,而只使用它在上一次迭代中留下的值。请更具体地说明“不工作”的含义?这对我来说似乎很好(虽然效率非常低)。对不起,我没有具体说明我在赶时间。我的意思是如果我加密:“abcdefghijklmnopqrstuvwxyz1234567890!@$%^&*()`-\=+[{]};':”,./?\\”我得到的输出是:“UTSRQPONMLKJIHGFEDCBA@?”=
  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);   
            }
        }

    }
}
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;
    }
}
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;
    }
}
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();
    }
}
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;
    }
}
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");
    }
}
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');
    }
}