方法字符密码编程问题

方法字符密码编程:

(1)编写方法int isalpha (char c),判断参数c是否是字符,如果是字符,返回1,否则返回0;

(2)编写方法char tolower(char c),判断参数c是否是小写字符,如果是小写字符,则转换该字符为大写字符;

(3)编写译码程序,将输入的字符串进行译码,规则如下:

1)调用方法isalpha (char c), 判别字符串是否是字母,如果是字母,则将字母转换为下一个序列,如a->b,A->B,b->c,B->C,z->a, Z->A;

2)调用方法tolower(char c) 将字符串中的小写字母转换为大写字母

3)其他字符原样输出;

例如: 输入: abcz!&10ABCZ

         输出:  BCDA!&10BCDA

就是不是很懂

实现如下,望采纳:

// 检查字符是否为字母的方法
public static int isalpha(char c) {
    if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
        return 1;
    }
    return 0;
}

// 将字符转换为小写的方法
public static char tolower(char c) {
    if (c >= 'A' && c <= 'Z') {
        return (char)(c + 32);
    }
    return c;
}

// 解码字符串的方法
public static String decode(String s) {
    StringBuilder decoded = new StringBuilder();
    for (int i = 0; i < s.length(); i++) {
        char c = s.charAt(i);
        if (isalpha(c) == 1) {
            if (c == 'z') {
                decoded.append('a');
            } else if (c == 'Z') {
                decoded.append('A');
            } else {
                decoded.append((char)(c + 1));
            }
        } else {
            decoded.append(tolower(c));
        }
    }
    return decoded.toString();
}

要使用这些方法,你可以以下面的方式调用它们:

int alpha = isalpha('a'); // alpha 设为 1
char lower = tolower('A'); // lower 设为 'a'
String decoded = decode("abcz!&10ABCZ"); // decoded 设为 "BCDA!&10BCDA"