运算符<不能应用于java.lang.String,char

时间:2016-02-18 22:21:44

标签: java encryption

让我解决这个问题,我是java的初学者。我的代码部分遇到了问题。

public class Cipher { 

private int secretKey;
private int superSecretKey;

/**
 *
 * @param key the variable used to shift the number of letter
 */
public Cipher(int key) { 
    secretKey = key;
    superSecretKey = key;
}

/**
 *
 * @param s the string to be encrypted
 * @return encrypted string
 */
public String caesarEncrypt (String s) {
    String r = "";

    for(int i = 0; i < s.length(); i++) {

        char c = (char) (s.charAt(i));
        if(Character.isLetter(c)) {
            if (Character.isUpperCase(c)) { 
                r += (char) ('A' + (c - 'A' + secretKey) % 26);
            }
            else {
                r += (char) ('a' + (c - 'a' + secretKey) % 26);
            }
        }
            else {
            r += c;
        }
    }
    return r;
}

/**
 *
 * @param s the string to be decrypted
 * @return the decrypted string
 */
public String caesarDecrypt (String s) {
    String r = "";

    for(int i = 0; i < s.length(); i++) {

        char c = (char) (s.charAt(i));
        if(Character.isLetter(c)) {
            if (Character.isUpperCase(c)) {
                int tmp = (char) ('A' + (c - 'A' - secretKey) % 26);
                if (tmp < 'A') r +=26;
            r += (char) (tmp);
            }
            else {
                int tmp = (char) ('a' + (c - 'a' - secretKey) % 26);
                if (tmp < 'A') r +=26;
            r += (char) (tmp);
            }
        }
            else {
            r += c;
        }
    }
    return r;
}

/**
 *
 * @param s the string to be encrypted using the augustus encryption
 * @return the encrypted string
 */
public String augustusEncrypt (String s) {
      String tmp = caesarEncrypt(s);
      String r = "";
      String newStringKey = Integer.toString(superSecretKey);
      int index = 0;

     for (int i = 0; i < s.length(); i++) {

         char c = (char) (s.charAt(i));
         if(Character.isLetter(c)) {
             char augKey = newStringKey.charAt(index++ % newStringKey.length());  
             if (Character.isUpperCase(c)) {
                 r += (char)('A' - (c - 'A' + Character.getNumericValue (augKey)) % 26);
             }
             else {
                 r += (char)('a' - (c - 'a' + Character.getNumericValue (augKey)) % 26);
         }      
     }
         else {
         r += c;
     }        
 }
 return r;
}

/**
 *
 * @param s the string to be decrypted using the augustus decryption
 * @return the decrypted string
 */
public String augustusDecrypt (String s) {
     String dec = caesarDecrypt(s);
     String r = "";
     String newStringKey = Integer.toString(superSecretKey);
     int index = 0;

 for (int i = 0; i < s.length(); i++) {

     char c = (char) (s.charAt(i));
         if(Character.isLetter(c)) {
             char augKey = newStringKey.charAt(index++ % newStringKey.length());  
             if (Character.isUpperCase(c)) {
                 r += (char)('A' - (c - 'A' - Character.getNumericValue (augKey)) % 26);
                 if (dec < 'A') r +=26;
            r += (char) (dec);
             }
             else {
                 r += (char)('a' - (c - 'a' - Character.getNumericValue (augKey)) % 26);
                 if (dec < 'A') r +=26;
            r += (char) (dec);
         }      
     }
         else {
         r += c;
     }        
 }
 return aug;
}



}

以上是整个代码,因为我需要修复的部分需要其他部分。下一段代码是我遇到问题的方法。

public String augustusDecrypt (String s) {
     String dec = caesarDecrypt(s);
     String r = "";
     String newStringKey = Integer.toString(superSecretKey);
     int index = 0;

 for (int i = 0; i < s.length(); i++) {

     char c = (char) (s.charAt(i));
         if(Character.isLetter(c)) {
             char augKey = newStringKey.charAt(index++ % newStringKey.length());  
             if (Character.isUpperCase(c)) {
                 r += (char)('A' - (c - 'A' - Character.getNumericValue (augKey)) % 26);
                 if (dec < 'A') r +=26;
            r += (char) (dec);
             }
             else {
                 r += (char)('a' - (c - 'a' - Character.getNumericValue (augKey)) % 26);
                 if (dec < 'A') r +=26;
            r += (char) (dec);
         }      
     }
         else {
         r += c;
     }        
 }
 return aug;
}

在for循环中,我收到操作员错误。这行代码完全正确。

if (dec < 'A') r +=26;

有人可以告诉我我做错了什么吗?

1 个答案:

答案 0 :(得分:0)

您正在尝试确定字符串(dec)是否小于字符('A'),这没有意义。我不清楚你的代码试图做什么。也许你打算做的是:

if (dec.charAt(i) < 'A') r +=26;

在任何情况下,您的第一个操作数都必须是字符类型。