所以我正在制作一个程序,您可以在其中使用关键字对消息进行编码或解码。我有编码部分,但我不确定如何解码消息。我是java新手,所以任何帮助将不胜感激!
到目前为止,这是我的代码:
import java.util.Scanner;
class Coder
{
public static void main(String[] arg)
{
boolean encode = false;
boolean decode = false;
int multi = 1;
Scanner inputdata=new Scanner(System.in);
System.out.print("Type E to encode or D to decode:\n");
String opt=inputdata.nextLine();
if (opt.equals("e")||opt.equals("E"))
{
System.out.print("Type the keyword to use for encoding:\n");
encode = true;
}
else if (opt.equals("d")||opt.equals("D"))
{
System.out.print("Type the keyword for decoding:\n");
decode = true;
}
else
{
System.out.print("Error:This is not an option");
}
if (encode==true)
{
String keyword=inputdata.nextLine();
int[] key = new int[1000];
for (int k = 0; k < key.length; ++k)
{
char c = keyword.charAt(k % keyword.length());
if (c >= 'a' && c <= 'z')
{
key[k] = c-'a';
}
}
System.out.print("Type encode message:\n");
String message=inputdata.nextLine();
for (int i = 0; i < message.length(); ++i)
{
if (message.charAt(i) >= 'a' && message.charAt(i) <= 'z')
{
System.out.println((int)message.charAt(i) - (int)'a' + key[i]);
}
else if (message.charAt(i) >= 'A' && message.charAt(i) <= 'Z')
{
System.out.println((int)message.charAt(i) - (int)'A' + key[i]);
}
else if (message.charAt(i) == ' ')
{
System.out.println(" ");
}
else
{
System.out.println(message.charAt(i));
}
}
}
if (decode==true)
{
}
}
}