计算凯撒密码的明文和密文之间的距离

Calculating distance between plaintext and ciphertext of a Caesar cipher

我有一个这样的文本文件:

GASDGHSDH BVNYBCNYC
HDFS UQSF
CXVN KFDV
SHDF APLN
HDSFHS OKZMOZ
SAH YGN
DSHF HWLJ
REEW TGGY
SGDFH AOLNP
DHSF EITG
GSDHASD IUFJCUF
DHFSDF KOMZKM
DSFH SHUW

而且我不知道解密密钥。例如: HDFS UQSF 键 == 13

因为 U - H == 13

好的。我写代码:

public static int codeJC(String first, String twice){
    first = first.toLowerCase();
    twice = twice.toLowerCase();
    char a = first.charAt(0);
    char b = twice.charAt(0);


    int r = 0;
    if((a-b) >0){
        r = a-b;
    }else{
        r = b-a;

    }

    return r;


}

但是如果:

System.out.println(codeJC("Z", "A")); //this return 25 !?!?!?!?
System.out.println(codeJC("C", "F")); //this return good value

为什么当 a < b 得到一个错误的值?

if ((a - b) > 0) {
    r = 26 - (a - b);
} else {
    r = b - a;
}