使用char数组和字符串来加密和解密Java

时间:2015-11-03 22:09:28

标签: arrays string methods while-loop java.util.scanner

我正在学习Java,我反对这个实验任务。我几乎完成了这个Assignment,所有方法都有效,但while循环只在选择2 - 1 - 2时才有效,我无法弄清楚这些代码会发生什么。 当while循环继续工作时,它跳过in.nextLine();或选择退出,它一直工作直到错误,我不知道发生了什么。

我先说出那些:

 Scanner in = new Scanner(System.in);
//declear what will use
 boolean isExit = true;
 int chose = 0, chose2 = 0, chose3 = 0, key = 0;
 char[] array;

then here is the while statement :

while (isExit) {
    System.out.println("Please input the message: ");
    String text = in.nextLine();

    System.out.println("Chose one for next\n"
    + " 1 to decrypt a message\n"
    + " 2 to encrypt a message. ");
    chose = in.nextInt();

    if (chose == 1) {
    System.out.println("Chose one for next\n"
    + " 1 for an entire message reversal decryption\n"
    + " 2 for a Pig-Latin decryption\n"
    + " 3 for a simple Caesar style decryption");
    chose2 = in.nextInt();
    if (chose2 == 1) {
    array = text.toCharArray();
    reverseEncryptandDecrypt(array);
    } else if (chose2 == 2) {
    pigLatinDecrypt(text);
    } else if (chose2 == 3) {
    array = text.toCharArray();
    System.out.println("Please input the key for decrypt: ");
    key = in.nextInt();
    simpleDecrypt(array, key);
    } else {
    System.out.println("Chose invaild");
    isExit = true;
    }
   } else if (chose == 2) {
    System.out.println("Chose one for next\n"
   + " 1 for an entire message reversal encryption\n"
   + " 2 for a Pig-Latin encryption\n"
   + " 3 for a simple Caesar style encryption");
   chose2 = in.nextInt();
   if (chose2 == 1) {
   array = text.toCharArray();
   reverseEncryptandDecrypt(array);
   } else if (chose2 == 2) {
   pigLatinEncrypt(text);
   } else if (chose2 == 3) {
   array = text.toCharArray();
   System.out.println("Please input the key for encrypt: ");
   key = in.nextInt();
   simpleEncrypt(array, key);
   } else {
   System.out.println("Chose invaild");
   isExit = true;
   }
  }else {
   System.out.println("Chose invaild");
   isExit = true;
   }
System.out.println("Chose one for next\n"
   + " 1 to continue with the program\n"
   + " 2 to quit the program");
  chose3 = in.nextInt();
  if (chose3 == 1) {
isExit = true;

} else if (chose == 2) {
 isExit = false;

 }
}

here is my method: 

static char [] reverseEncryptandDecrypt(char [] input){  //reverse input arrays
 //array = s.toCharArray();
    char output [] = new char [input.length];
    for(int i = input.length-1, j=0; i >=0;i--, j++){
    output[j] = input[i];
    }
    for(int i =0; i < output.length; i++){     //test this method
   System.out.print(output[i]);
    } System.out.println("");
     return output;    
    }
   static String pigLatinEncrypt(String input) {     // using isvowel to know add yay or other
   String output = null;
    int position = 0;
    if (IsVowel(input.charAt(0))) {
    output = input + "yay";
// System.out.println(output); // test
    return output;
   } else {
    while (position < input.length() && !IsVowel(input.charAt(position))) {
      position++;
   }
  String first = input.substring(position, input.length());
   String second = input.substring(0, position) + "ay";
   output = first + second;
   System.out.println(output); // test
   return output;
   }

    }

    static boolean IsVowel(char input) {     // compare what begain the word
        if (input == 'a') {
            return true;
        } else if (input == 'e') {
            return true;
        } else if (input == 'i') {
            return true;
        } else if (input == 'o') {
            return true;
        } else if (input == 'u') {
            return true;
        } else {
            return false;
        }
    }

    static String pigLatinDecrypt(String input) {    //remove the last some char and fix the words
        String output = null;
        if (input.endsWith("yay")) {
            output = input.substring(0, input.length() - 3);
            System.out.println(output);  //test
            return output; 
        } else {
            input = input.substring(0, input.length() -2);
            char[] charArr = input.toCharArray();
            char [] ch = new char [charArr.length];

            char last = charArr[charArr.length - 1];
            charArr[charArr.length - 1] = charArr[0];
            //System.out.println(last);
            for(int i = charArr.length-1; i>=1; i--){

                ch[i]=charArr[i-1];
            }
            ch[0] = last;

            output = new String(ch);
            System.out.println(output);//test
            return output;
        }
    }

    static char [] simpleEncrypt(char [] input, int key){   //add key to every char [i]
    //char [] output = new char [input.length];
    for(int i =0; i<input.length;i++){
        input[i]+=key;
    }
    for(int i =0; i<input.length;i++){
        System.out.print(input[i]);
    }
    return input; 
    }

    static char [] simpleDecrypt(char [] input, int key){   //remove same key from each char[] element
    //char [] output = new char [input.length];
    for(int i =0; i<input.length;i++){
        input[i]-=key;
    }
    for(int i =0; i<input.length;i++){
        System.out.print(input[i]);
    }
    return input; 
    }

0 个答案:

没有答案