简体   繁体   English

无法将ascii 121打印到字符y,而是打印特殊字符

[英]Can't print ascii 121 to character y, instead printing a special character

Given a String, I have to find the ASCII of each character in the string. 给定一个字符串,我必须找到字符串中每个字符的ASCII。 If any of the ASCII's are not prime, it must be converted to the nearest prime ASCII. 如果任何ASCII都不是素数,则必须将其转换为最接近的素数ASCII。 If two prime ASCII's are equidistant from the original ASCII, the lower is taken. 如果两个素数ASCII码与原始ASCII码等距,则取较低者。 After the above operation I need to convert the resulting ASCII to a String. 完成上述操作后,我需要将结果ASCII转换为字符串。

But my code's problem is it can't print y form ASCII 121 但是我的代码的问题是它不能以ASCII 121格式打印

Please help me finding the error; 请帮助我发现错误; Thanks in advance 提前致谢

    int T = s.nextInt();

    int len;
    byte lowerPrime, greaterPrime, temp;
    String word;
    byte[] b;
    for (int i = 0; i < T; i++) {
        len = s.nextInt();
        s.nextLine();
        word = s.nextLine();

        b = word.getBytes(StandardCharsets.US_ASCII);

        for (int k = 0; k < b.length; k++) {
            temp = b[k];
            if (!checkPrime(temp)) {
                lowerPrime = findPrimeSmaller(temp);
                greaterPrime = findPrimeGreater(temp);

                if ((temp - lowerPrime) == (greaterPrime - temp)) {
                    b[k] = lowerPrime;
                } else if ((temp - lowerPrime) < (greaterPrime - temp)) {
                    b[k] = lowerPrime;
                } else if ((temp - lowerPrime) > (greaterPrime - temp)) {
                    b[k] = greaterPrime;
                }
            }
        }

        System.out.println(new String(b, "UTF-8"));

    }

}

private static boolean checkPrime(byte n) {

    if (n == 1) {
        return false;
    }

    byte i = 2;

    while (i < (n / 2)) {
        if (n % i == 0) {
            return false;
        }
        i++;
    }
    return true;
}

private static byte findPrimeGreater(byte n) {
    while (!checkPrime(n)) {
        n++;
    }
    return n;
}

private static byte findPrimeSmaller(byte n) {
    while (!checkPrime(n)) {
        n--;
    }
    return n;
}

Your code is working as intended. 您的代码按预期工作。 Here an online compiler with a few added debug-lines. 这里是一个在线编译器,其中添加了一些调试行。

As you can see, it will find 127 for the character y ( 121 ) in the input, being the first greater prime. 如您所见,它将在输入中找到字符y121 )的127 ,是第一个大的质数。 The ASCII character with code-point 127 is an unprintable character however , which makes it appear as if there isn't a character in the output. 但是,代码点为127的ASCII字符是不可打印的字符 ,这使其看起来好像输出中没有字符。

You may want to either restrict your code to the printable ASCII range [32,126] , by adding some checks to the findPrimeSmaller and findPrimeGreater methods so they don't go beyond this limit (of course, don't return that limit, since it isn't a prime, but return Integer.MIN_VALUE and Integer.MAX_VALUE or something like that instead when it has reached the boundary, so you know it will pick the other prime instead in your if-checks). 您可能希望通过将一些检查添加到findPrimeSmallerfindPrimeGreater方法中来将代码限制为可打印的ASCII范围[32,126] ,这样它们就不会超出此限制(当然,不要返回该限制,因为它不是不是素数,但是在到达边界时返回Integer.MIN_VALUEInteger.MAX_VALUE或类似的东西,所以您知道它会在if-check中选择其他素数)。
Or you may want to skip unprintable characters if you want to non-ASCII unicode characters in your program. 或者,如果要在程序中使用非ASCII Unicode字符,则可能要跳过不可打印的字符。

Also, a general note: using an IDE debugger or adding print lines like I did in the online version above is helpful in checking where the code goes wrong and what it does. 另外,请注意:使用IDE调试器或添加打印行(如我在上面的在线版本中所做的那样)有助于检查代码的错误位置和功能。 In this case your code works completely fine, but the resulting character simply is an unprintable character (with unicode value 127 ). 在这种情况下,您的代码可以完全正常工作,但是生成的字符只是一个不可打印的字符(unicode值为127 )。

PS: This part of your code can be simplified: PS:您的代码的这一部分可以简化:

if ((temp - lowerPrime) == (greaterPrime - temp)) {
    b[k] = lowerPrime;
} else if ((temp - lowerPrime) < (greaterPrime - temp)) {
    b[k] = lowerPrime;
} else if ((temp - lowerPrime) > (greaterPrime - temp)) {
    b[k] = greaterPrime;
}

To: 至:

if ((temp - lowerPrime) <= (greaterPrime - temp)) { // Single <=, instead of loose == and <
    b[k] = lowerPrime;
} else if ((temp - lowerPrime) > (greaterPrime - temp)) {
    b[k] = greaterPrime;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM