我正在尝试使用StringBuilder创建caeser密码,但我有两个主要问题。一种是我的加密和解密不会互相抵消,另一种是我使用.isLetter()函数时,它实际上跳过了很多字符,而不仅仅是忽略空白。谁能帮我?
import java.io.*;
import java.util.Scanner;
import javafx.scene.transform.ScaleBuilder;
public class FileUtilities
{
static String tempE ;
static String tempD ;
public static void main(String [] args)
{
try
{
File iFile = new File("BattlePlans.txt");
File oFile= new File("decodedBattlePlans.txt");
Scanner input = new Scanner(iFile);
PrintWriter fout = new PrintWriter(oFile);
while(input.hasNextLine())
{
tempE = input.nextLine();
//System.out.println(args[0]);
//StringBuilder str = new StringBuilder(args[1]);
//My encrypt funtion give me an indexoutofbounds error for some reason and I don't know why do you see what I did wrong
if(args[0].equals("encrypt"))
{
fout.println(encrypt(tempE, Integer.parseInt(args[1])));
}else if(args[0].equalsIgnoreCase("decrypt"))
{
fout.println(decrypt(tempE, Integer.parseInt(args[1])));
}
}
input.close();
fout.close();
}
catch(FileNotFoundException e)
{
}
// this is commented out becuase I couldn't figure out
//how to make the InvalidCommand method or InvalidShift methods
//and it was keeping it from running
//do I even have to make my own, or is there something built in I can use?
/*catch(InvalidCommand i)
{
System.out.println("Invalid command");
}
catch(InvalidShift s)
{
System.out.println("Invalid shift");
}*/
}
public static String encrypt(String encrypt, int shift)
{
//first SB takes in the original text and second SB holds the changed txt
StringBuilder str = new StringBuilder(encrypt);
StringBuilder str2 = new StringBuilder();
for(int i = 0; i < str.length(); i++ )
{
// I want to use a isLetter() if statement here to that it won't take out my space characters but when I did it made it print nothing so I deleted it
char s = encrypt.charAt(i);
//checks for capital and lowercase letter to see what the shift range is
if(Character.isLetter(i))
{
if(s >= 'A' && s <= 'Z')
{
//I went to the ILC to ask how to handle the shift and I could just add it to the end
//I was told that I couldn't and had to use another int and subtract the bottom of the range from the character at the index i
//the subtract the shift. It shifts the text to the left actually. is that okay?
int x = s - 'A' - shift;
x = x % 26;
str2.append((char)(x + 'A'));
// add to str with .append
}else if(s >= 'a' && s <= 'z')
{
int z = s - 'a' - shift;
z = z % 26;
str2.append((char)(z + 'a'));
}
}
}
return str2.toString();
}
public static String decrypt(String decrypt, int shift)
{
//first SB takes in the original text and second SB holds the changed txt
StringBuilder str = new StringBuilder(decrypt);
StringBuilder str2 = new StringBuilder();
for(int i = 0; i < decrypt.length(); i++)
{
char s = decrypt.charAt(i);
//same if statement for encrypt would go here to so that it would keep the whitespaces
if(Character.isLetter(i))
{
if(s >= 'A' && s <= 'Z')
{
//I went to the ILC to ask how to handle the shift and I could just add it to the end
//I was told that I couldn't and had to use another int and subtract the bottom of the range from the character at the index i
//the subtract the shift. It shifts the text to the left actually. is that okay?
int x = s + 'A' + shift;
if(x < 0)
x = x + 26;
str2.append((char)(x + 'A'));
// add to str with .append
}else if(s >= 'a' && s <= 'z')
{
int z = s + 'a' + shift;
if(z < 0)
z = z + 26;
z = z % 26;
str2.append((char)(z + 'a'));
}
}
}
//decrypt is encrypt backwards
return str2.toString();
}
}
最佳答案
一些提示:
测试Character.isLetter(s)
,而不是Character.isLetter(i)
。当前,您正在测试符号的索引,而不是符号本身。
确保您正在摄取的mod 26
是正数,在计算+26
之前将x
添加到x = x % 26
。
为什么有两个单独的encrypt
和decrypt
方法?消除其中之一。isLetter
是不必要的,无论如何都要检查>= 'A'
和<= 'Z'
。