更新从ORS更改一个部分
我正在尝试编写一个Cipher程序,到目前为止,它加密了到目前为止的字母,但我无法忽略非字母字符。我有一个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
答案 0 :(得分:1)
更改if
编辑:
if ((asciiArray[count] < 65 || asciiArray[count] > 90) && (asciiArray[count] < 96 || asciiArray[count] > 122))
答案 1 :(得分:1)
问题似乎在于这一行:
if (asciiArray[count] < 65 && asciiArray[count] > 90 && asciiArray[count] < 96 && asciiArray[count] > 122)
你是说如果一个数字小于65,它大于90,如果它小于96且大于122 ...你需要用OR替换那些AND条件,因为它是现在,它总是评估为假。
此外,另一种解决方法是,由于charArray
包含字符值,您可以使用Character.isLetter(char ch)
代替,因此您的代码将变为:
if(Character.isLetter(charArray[count])){
asciiArray[count] = ((asciiArray[count]- 97)+ shift) % 26 + 97;
}
请注意,由于您的程序现在正在使用,{
阻止了else
。{/ p>