単語を暗号化し、ユーザーが入力した文字を特定の数だけシフトし、その暗号を解読し、単語が回文であるかどうかを確認できる単語ゲームを作成しようとしています。問題は、入力を維持する方法がわからないため、暗号化または回文であるかどうかを確認した後、プログラムが終了するため、暗号化されたものを復号化できません。
たとえば、ユーザーが「hello」という単語を入力し、キー 3 を使用してその単語を暗号化することを選択した場合、「khoor」と表示されます。次に、「khoor」を「hello」に復号化して続行できるようにしたいのですが、プログラムは終了します。
また、ユーザーがキー番号を入力すると、文字は入力番号よりも 7 文字多くシフトします。
import java.util.Scanner;
public class WordPlayTester{
public static void main(String [] args){
String word, reverse="";
String original;
int key= 0;
String Menu= "1-Encrypt \n2-Decrypt \n3-Is Palindrome \n0-Quit \n-Select an option-";
Scanner in = new Scanner(System.in);
System.out.println("-Type any word-");
String toUpperCase;
word = in.nextLine();
System.out.println(Menu);
int choice=in.nextInt();
if(choice==1)
{
System.out.println("Insert a Key number");
int select= in.nextInt();
for (int i=0; i < word.length(); i++) {
char c = word.charAt(i);
if (c >= 'A' && c <= 'z') {
c = (char)(c - 65);
int n = c+select;
n = n % 26;
if (n < 0) {
n = n + 26;
}
c = (char)(n + 65);
}
System.out.print(c);
}
}
else if(choice==2)
{
System.out.println("Insert a Key number");
int select2= in.nextInt();
for (int i=0; i < word.length(); i++) {
char c = word.charAt(i);
if (c >= 'A' && c <= 'z') {
c = (char)(c - 65);
int n = c+select2;
n = n % 26;
if (n < 0) {
n = n - 26;
}
c = (char)(n - 65);
}
System.out.print(c);
}
if(key==0)
{
System.out.println("Word has not been encrypted yet.");
}
}
else if(choice==3)
{
int length = word.length();
for ( int i = length - 1 ; i >= 0 ; i-- )
reverse = reverse + word.charAt(i);
if (word.equals(reverse))
System.out.println("Your word is a palindrome.");
else
System.out.println("Your word is not a palindrome.");
}
else if(choice==0)
{
System.exit(0);
}
else
{
System.out.println(Menu);
}
}
}
ありがとうございました