これを邪魔にならないようにしましょう。私は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;
}
}
上記はコード全体です。修正が必要な私の1つの部分には、他の部分が必要だからです。この次のコードは、私が問題を抱えているメソッドです。
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;
いずれの場合も、最初のオペランドは文字タイプである必要があります。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加