Java 我在这里加密错了什么?

Java 我在这里加密错了什么?,java,encryption,character,Java,Encryption,Character,所以我有一个文字游戏项目要做,我必须加密一些字符。当我运行它并键入1进行加密时,它不会移动那么多字母。它只是把工作再打印一遍。我想知道我能做些什么来修复它,如果我说“你好”,它会打印出一个字符,然后说“Ifmp”谢谢 import java.util.Scanner; public class WordPlayTester{ public static void main(String [] args){ String word, reverse=""; Strin

所以我有一个文字游戏项目要做,我必须加密一些字符。当我运行它并键入1进行加密时,它不会移动那么多字母。它只是把工作再打印一遍。我想知道我能做些什么来修复它,如果我说“你好”,它会打印出一个字符,然后说“Ifmp”谢谢

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


  }
}
import java.util.Scanner;
公共类WordPlayTester{
公共静态void main(字符串[]args){
字符串字,反向=”;
字符串原件;
int键=0;
String Menu=“1-Encrypt\n2 Decrypt\n3是回文\n0 Quit\n-选择一个选项-”;
扫描仪输入=新扫描仪(系统输入);
System.out.println(“-Type any word-”);
word=in.nextLine();
系统输出打印项次(菜单);
int choice=in.nextInt();
如果(选项==1)
{
System.out.println(“插入密钥号”);
int select=in.nextInt();
for(int i=0;i='A'&&c=0;i--)
反向=反向+字字符(i);
if(字等于(反))
System.out.println(“您的单词是回文。”);
其他的
System.out.println(“您的单词不是回文。”);
}
else if(选项==0)
{
系统出口(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');
  }
}
import java.util.Scanner;
公共类WordPlayTester{
公共静态void main(字符串[]args){
字符串字;
int键=0;
扫描仪输入=新扫描仪(系统输入);
System.out.println(“-Type any word-”);
word=in.nextLine();
System.out.println(“插入密钥号”);
int select=in.nextInt();
for(int i=0;i如果(c>='A'&&c你减去64;你加65。对吗?我认为这是我认为正确的,是不是错了?那么宗正礼,你认为我应该把它们改成小写而不是大写,并且应该在单词上加一个字符?你提示
选择
,但不要在计算中使用它,这是一个疏忽错误。什么时候我使用输入Byzantiumablone运行它,输出是每行打印一个字符DABCPVKWOCDGNQPG。因此,即使您添加1,您也实现了移位2的效果。-64和+65之间的不对称确实很重要。减法和加65(aka
(int)'A'
)使代码在大写字母上正常工作。此外,为了将来参考,最好提交一份SSCCE()。在这种情况下,您的代码不会询问要执行哪个操作;它只会执行加密,因为这就是您的问题所在。这减少了代码需要帮助您的人进行研究。谢谢,但是当您键入小写字母时,它不起作用,是否有什么方法可以解决此问题?我接受了您所做的,它只尝试映射大写ASCII字母,并使其正确工作。显然,您可以为小写字母创建一个并行代码块,但它非常难看。您还可以简化现有的代码。至少,我希望创建一个函数来执行两个转换(低、高、选择和c作为参数)。我希望Java与C
isupper
/
islower
函数有类似之处;请使用它们。当然,像这样的凯撒密码不是安全性的缩影。有一种变体称为ROT13,对应于13的密钥号。