1つのセグメントを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