让我解决这个问题,我是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;
无论如何,您的第一个操作数必须是字符类型。
关于java - 运算符 < 不能应用于 java.lang.String,char,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35493617/