有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java ascii shift程序几乎适用于整个字符串

我必须编写一个程序(方法thingy,恕我直言),该程序有一个字符数组作为输入,根据其ascii值将其转换为数字,将其移位到输入的移位值(可以是正数或负数)是什么,然后对其进行解密

我的程序的问题是,它几乎适用于整个数组,但对于某些测试用例,在最后几个字符中失败。有人能帮我指出我逻辑上的缺陷吗

例如,解密后sgADF@#$5^^%{].读取sgADF@#$5^^%{g3(即].替换为g3

我很确定我的解密没有问题。它只是将行数组(应该是char)输入到具有相反移位值的encrypt中

//converts char values to numbers and stores them in hold array
    for (int x = 0; x < line.length; x++) {
        char character = line[x];
        int ascii = (int) character;
        hold[x] = ascii;
    }
[![enter image description here][1]][1]//shift to numbers
    for (int x = 0; x < line.length; x++) {

        //case1
        if (nshift < 0) {
            while (hold[x] + nshift < THIRTY_TWO) {
                nshift = nshift + NINETY_FIVE;
                    // nshift = copy of shift (checkstyle)
            }
            hold[x] = hold[x] + nshift;

        }


        //case2,
        if (shift > 0) {
            while (hold[x] + nshift > ONE_TWENTY_SIX) {
                nshift = nshift - NINETY_FIVE;
                    // nshift = copy of shift (checkstyle)
            }

            hold[x] = hold[x] + nshift;

        }

    }
    for (int x = 0; x < hold.length; x++) {
        char digit = (char) hold[x];
        out[x] = digit;
    }

    return out;

shows tests casesenter image description here


共 (0) 个答案