0

更新将一个段从 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 
4

2 回答 2

1

改变你内心的状况if

编辑:

if ((asciiArray[count] < 65 || asciiArray[count] > 90) && (asciiArray[count] < 96 || asciiArray[count] > 122))

于 2012-10-24T05:01:49.127 回答
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块中放错了位置。

于 2012-10-24T05:02:44.270 回答