更新将一个段从 ANDS 更改为 ORS
我正在尝试编写一个密码程序,到目前为止,它加密了字母,但我无法让它忽略非字母字符。我有一个应该处理的 if 语句,但似乎不起作用:
import javax.swing.*;
import java.text.*;
import java.util.*;
import java.lang.*;
public class Cipher {
private String phrase; // phrase that will be encrypted
private int shift; //number that shifts the letters
///////////////
//Constructor//
//////////////
public Cipher( int new_shift)
{
shift = new_shift;
}//end of cipher constructor
////////////
//Accessor//
////////////
public int askShift() {
return shift;
}//end of askShift accessor
////////////
//mutators//
////////////
public void changeShift (int newShift) {
shift = newShift;
}//end of changeShift mutator
/////////////
//instances//
/////////////
public String encryptIt(String message) {
char[] charArray = message.toCharArray(); //converts to a character array
int[] asciiArray = new int[charArray.length]; //array to store the ascii codes
//for loop that converts the charArray into an integer array
for (int count = 0; count < charArray.length; count++) {
asciiArray[count] = charArray[count];
} //end of For Loop
//loop that performs the encryption
for (int count = 0; count < asciiArray.length; count++) {
//these numbered equality statements check to see if the ascii code is a non-character. If it is, continue the loop
if (asciiArray[count] < 65 || asciiArray[count] > 90 || asciiArray[count] < 96 || asciiArray[count] > 122){
continue;
}
else
asciiArray[count] = ((asciiArray[count]- 97)+ shift) % 26 + 97;{
}
} //end of for loop
//loop that converts the int array back into a character array
for (int count = 0; count < asciiArray.length; count++) {
charArray[count] = (char)asciiArray[count];
}
/* commenting out this block until futher notice
//loop that performs the encryption
for (int count = 0; count < charArray.length; count++) {
int shiftNum = 2;
charArray[count] = (char)(((charArray[count] - 'a') + shiftNum) % 26 + 'a');
} // end of for loop */
message = new String(charArray); //converts the array to a string
return message;
}//end of encrypt instance
//////////
///Main///
//////////
public static void main(String[] args) {
Cipher cipher = new Cipher(1); //cipher with a shift of one letter
Cipher cipher2 = new Cipher(5); //cipher with a shift of two letters
String phrase = JOptionPane.showInputDialog(null, "Enter phrase to be messed with ");
String encryption = cipher.encryptIt(phrase);
String encryption2 = cipher2.encryptIt(phrase);
JOptionPane.showMessageDialog(null, encryption);
JOptionPane.showMessageDialog(null, encryption2);
}//end of main function
} //end of cipher class