Java 从for循环中获取结果,并为每次迭代将其添加到另一个变量中

Java 从for循环中获取结果,并为每次迭代将其添加到另一个变量中,java,encryption,caesar-cipher,Java,Encryption,Caesar Cipher,我有一个任务,我应该写一个程序,可以使用用户输入的“加密密钥”对用户输入的单词进行加密和解密。我想做的是检查enter键中每个字母的值,与字母表(例如:a=1,b=2,c=3)进行比较,然后添加字母值,然后使用这些值来移动它们指定的单词中的字符。 这是到目前为止我分配任务的代码: /* Validity Check from http://stackoverflow.com/questions/33965542/password-check-program-checking-capita

我有一个任务,我应该写一个程序,可以使用用户输入的“加密密钥”对用户输入的单词进行加密和解密。我想做的是检查enter键中每个字母的值,与字母表(例如:a=1,b=2,c=3)进行比较,然后添加字母值,然后使用这些值来移动它们指定的单词中的字符。 这是到目前为止我分配任务的代码:

    /* Validity Check from http://stackoverflow.com/questions/33965542/password-check-program-checking-capitals-lowercase-letters-digit-and-special
 * 
 * 
 * 
 * 
 */
import java.util.*;
public class SecretDecoderFinal
{
  public static void main (String [] args)
  {
    optInput();

  }

  public static int optInput()
  {
    int opt = 0;
    do {
      String word = "";
      String key = "";
      System.out.println("Welcome to Seegson Security secure transmission terminal");
      System.out.println("");
      System.out.println("Enter an option for the terminal");
      System.out.println("(1) to encrypt a word");
      System.out.println("(2) to decrypt a word");
      System.out.println("(0) to exit the terminal");
      opt = In.getInt();
      System.out.println("You selected: Option " +opt);
      System.out.println("");
      switch(opt)
      {
        case 1:
          encryptWord(word, key);
          break;
        case 2:
          decryptWord(word, key);
          break;
        case 0:
          System.out.println("Thank you for using the encryption/decryption program");
          break;
        default:
          System.err.println("Invalid option. Select 1 for encryption, 2 for decryption, or 0 to exit");
          System.out.println("");
          break;
      }
    }while (!isExit(opt));

    return opt;

  }

  public static String keyInput(String key)
  {


    do
    {
      System.out.println("Enter the key you want for encryption/decryption");
      key = In.getString();
      System.out.println("Your key is: " +key);
      System.out.println("");

    }while (!isValid(key));
    //printEncrypted(key);

    return key;

  }

  public static String wordInput(String word)
  {

    do
    {
      System.out.println("Enter the word you want decrypted/encrypted");
      word = In.getString();
      System.out.println("You entered: " +word);
      System.out.println("");
    } while (!isValid(word));
    //printEncrypted(word);
    return word;

  }

  public static void encryptWord(String word, String key)
  {
   // String alphabet1 = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";

    wordInput(word);
    keyInput(key);
    System.out.println("The word from the encryptWord metod " +word);
    printEncrypted(word, key);

  }

  public static void decryptWord(String w, String k)
  {
    String alphabet1 = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
    wordInput(w);
    keyInput(k);

  }
这部分代码来自我的朋友

public static String printEncrypted(String word, String key)
  {
    System.out.println("This is the word from the printEncrypted method: " +word);

    int shift = 0;

    //Uses the key length to determine how much the alphabet will shift
    for (int x = 0; x < key.length(); x++) 

      shift += key.charAt(x) - 96; 
    shift = shift % 26;


    //Creates an array to perform the shift
    char [] y = word.toCharArray(); 

    for (int x = 0; x < y.length; x++) 
      //Uses the shift to shift the alphabet to decrypt the word.
      for (int d = 0; d < shift; d++)

      if (y[x] == 'z') 

      y[x] = 'a';

    else {
      y[x]--;
    } 
    String newWord = new String(y);
    System.out.println("Encrypted is " +newWord);
    return newWord;


  }

  public static boolean isValid (String s)
  {
    String strSpecialChars="!@#$%&*()_+=-|<>?{}[]~";
    //String alphabet2 = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
    boolean upCase = false;
    boolean isDigit = false;
    boolean spChar = false;

    if (s.matches(".+[A-Z].+")){
      upCase = true;
    }

    if (s.matches(".+[1-9].+")){
      isDigit = true;
    }

    if (strSpecialChars.contains(s)){
      spChar = true;
    }


    if (upCase|| isDigit || spChar)

    {
      System.err.println("The string cannot contain capital letters, special characters or numbers. Try again.");
      System.out.println("");
      return false;
    }
    else 
    {
      return true;
    }

  }


  public static boolean isExit (int option)
  {
    if (option == 0)
    {

      return true;
    }
    else
    {
      return false; 
    }
  } 

}
公共静态字符串打印加密(字符串字、字符串密钥)
{
System.out.println(“这是printEncrypted方法中的单词:“+word”);
int-shift=0;
//使用键的长度确定字母表的移位量
对于(int x=0;x
这就是我试图为我的角色转变所做的:

public class LetterTester
{
  public static void main (String []args)
  {

    String input="craig";
    final String alphabet="abcdefghijklmnopqrstvwxyz";
    int finalValue = 0;
    int[] numbers;
    for(int i=0;i<input.length();i++){

      finalValue=(alphabet.indexOf(input.charAt(i))+1);
      System.out.print(finalValue);

    }

  }
}
公共类字母测试器
{
公共静态void main(字符串[]args)
{
字符串输入=“craig”;
最终字符串字母表=“abcdefghijklmnopqrstvwxyz”;
int finalValue=0;
int[]数字;

对于(int i=0;i,OP的问题中有太多的内容无法完全回答。以下代码是解决方案空间的指南

  • 分别收集单词和键的输入以及任何其他操作。方法
    getWord()
    getKey
    将需要使用OP提到的
    类中的
    。这里的要点是,这些方法不需要任何参数,有一个返回,只收集信息(从而将输入与处理分离)
  • encryptWord(String,String)
    方法接受收集的输入并以某种方式对其进行转换,返回转换。它不应该做任何输出(同样,将I/O与处理分离)。我没有尝试复制加密算法
  • decryptoword(String,String)
    方法也会获取收集的输入并按照算法进行转换。这里没有尝试实现任何东西,但本质上它是加密的反面
  • 实际上并不是所有的东西都应该是静态的,但它遵循OP的方法
  • isValid(String)
    似乎仅限于小写字母。显然,如果该假设不正确,可以对其进行调整

    //  
    // is valid checks to see if only lower case characters  
    //  
    private static boolean isValid(final String chkWord)
    {
      // returns true only if the match is lower case a-z
      return chkWord.matches("^[a-z]+$");
    }
    
    private static String encryptWord(String word, String key)
    {
      // TODO: implement the encryption algorithm;
      //   The algorithm would use the key to do the encryption;
      // here will just add to character as quick example
      char[] wordChars = word.toCharArray();
    
      for (int i = 0; i < wordChars.length; ++i) {
          char c = wordChars[i];
          if (c >= 'a' && c <= 'm') { c += 13; }
          else if (c >= 'n' && c <= 'z') { c -= 13; }
          wordChars[i] = c;
      }
    
      return new String(wordChars);
    }
    
    private static String decryptWord(String word, String key)
    {
      // TODO: implement the decryption algorithm
      return "NEED TO IMPLEMENT";
    }
    
    private static String getWord()
    { 
      // the word should be gathered from the user in some fashion
      // using the In class that is provided
      return "helloworld"; 
    }
    
    private static String getKey()
    {
      // the key should be gathered from the user in some fashion
      // using the In class that is provided
      return "doit";
    }
    
    public static void main(String[] args)
    {
      final String word = getWord();
      final String key = getKey();
    
      boolean validWord = isValid(word);
      System.out.printf("Is valid [%s]: %b%n", word, validWord);
    
      if (! validWord) {
        System.err.println("Not a good word!");
        return;
      }
    
      String encrypted = encryptWord(word, key);
      System.out.printf("Encrypted %s: %s%n", word, encrypted);
    
      String decrypted = decryptWord(word, key);
      System.out.printf("Encrypted %s: %s%n", word, decrypted);
    
    }
    
    //
    //是否有效检查是否只有小写字符
    //  
    私有静态布尔值isValid(最终字符串chkWord)
    {
    //仅当匹配为小写a-z时返回true
    返回chkWord.matches(“^[a-z]+$”;
    }
    专用静态字符串加密字(字符串字、字符串键)
    {
    //TODO:实现加密算法;
    //算法将使用密钥进行加密;
    //这里将添加到角色作为快速示例
    char[]wordChars=word.tocharray();
    for(int i=0;i如果(c>='a'&&c='n'&&c WRT到
    wordInput()
    问题,则必须为该方法的返回值赋值,例如
    String wordToEncrypt=wordInput();
    。不需要传递参数,因为它读取用户的输入并返回值。我不明白包含所有注释掉的代码的目的是什么。为什么会有这么多空行?请整理您的代码。@KevinO谢谢!我会尝试的。@LiranFunaro谢谢,我清理了它。@KevinO您能澄清一下吗关于我可以改变什么?我已经分配了变量字符串word,它调用wordInput();变量字符串key,它调用keyInput()。你能给我举个例子吗?另外,我的isValid检查器给了我一个错误,还有wordInput,说找不到变量word,尽管我在上面声明了它。谢谢!我已经完成了你建议的更改,以及所有事情(好的,参数和方法调用)工作正常。加密和解密工作正常,但这不是我打算使用的方法。这是完全不同的,但我会等待答案。这是我的代码中的内容:现在我已经完成了您建议的更改,有效性检查程序不会发现输入中的错误,例如特殊字符或大写字母。它工作正常before@AliHammad,我没有遵循所需的加密/解密,所以他们说这是留给读者的练习。我只是加入了一些东西以获得一个占位符。从你的“朋友”那里,你可能得到了加密片段,但我不知道它是否正确。@AliHammad,WRT to the
    isValid()
    上面显示的方法:“Hello”=false;