簡體   English   中英

運算符<無法應用於java.lang.String,char

[英]operator < cannot be applied to java.lang.String,char

讓我解決這個問題,我是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;

有人可以告訴我我做錯了嗎?

您正在嘗試確定字符串(dec)是否小於字符('A'),這沒有意義。 我不清楚您的代碼正在嘗試做什么。 也許您想做的是:

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

無論如何,您的第一個操作數必須是字符類型。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM