-3
/** Yields: a String that contains each capital Letter (in 'A'..'Z') whose representation is prime */

public static String primeChars() {
    String s = "";

    // inv: s contains each capital in "A'..c-1 whose representation is prime

    for (char c = 'A'; c <= 'Z'; c=(char)(c+1)) {
        if (Loops.isPrime((int)c) == true) {
            s= s+1;
        }
    }

    // s contains each capital in 'A' ..'Z' whose rep is a prime

    return s; 
}
4

2 回答 2

4

它应该是:

s = s + c;

如果它是素数,你想连接字符。

于 2012-04-04T05:14:40.183 回答
0

也许你想写

s = s + c

那么你想在 char ASCII 码或 char 位置上评估 isPrime 吗?也就是说,您希望 'A' 的值是 65(ASCII 码)还是 1(因为 A 是 1 个字母)?在第一种情况下修复

s = s + c

已经足够了,但第二次你也需要改变

if (Loops.isPrime((int)c)==true) {

并且不要通过“c”但是

if (Loops.isPrime( (int)c - (int)'A' + 1 )==true) {
于 2012-04-04T05:25:04.523 回答