1

所以我有一个文字游戏项目要做,我必须加密一些字符。我正处于被卡住的地步,当我运行它并输入 1 进行加密时,它不会移动那么多字母。它只是重新打印工作。我想知道我能做些什么来修复它,如果我说“你好”,它会打印 1 个字符并说“ifmmp”谢谢!

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-");
          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 - 64);
                int n = c+1;
                n = n % 26;
                if (n < 0) {
                   n = n + 26;
                }
                c = (char)(n + 65);
             }
             System.out.println(c);
          }
          }


       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);
          }


  }
}
4

1 回答 1

0

这是一些经过修改的简化代码:

import java.util.Scanner;

public class WordPlayTester {

  public static void main(String [] args) {

    String word;
    int key= 0;

    Scanner in = new Scanner(System.in);
    System.out.println("-Type any word-");
    word = in.nextLine();

    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 - (int)'A');
        int n = c+select;
        n = n % 26;
        if (n < 0) {
          n = n + 26;
        }
        c = (char)(n + (int)'A');
      }
      System.out.print(c);
    }
    System.out.print('\n');
  }
}

给定一个输入 BYZANTIUM 和密钥 1,它输出 CZABOUJVN;给定密钥 2,它输出 DABCPVKWO;给定密钥 25,它输出 AXYZMSHTL;给定键 26,它输出 BYZANTIUM。

于 2013-10-22T20:41:28.663 回答