处理非字母字符的Java程序

处理非字母字符的Java程序,java,encryption,char,int,ascii,Java,Encryption,Char,Int,Ascii,更新以将一段从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

更新以将一段从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 
import javax.swing.*;
导入java.text.*;
导入java.util.*;
导入java.lang.*;
公共类密码{
私有字符串短语;//将被加密的短语
private int shift;//移动字母的数字
///////////////
//建造师//
//////////////
公共密码(int new_shift)
{
班次=新班次;
}//密码构造函数的结尾
////////////
//存取器//
////////////
public int askShift(){
返回移位;
}//askShift存取器的结束
////////////
//突变子//
////////////
公共无效转换(int新闻转换){
班次=新班次;
}//变换移位变异体结束
/////////////
//实例//
/////////////
公共字符串加密(字符串消息){
char[]charArray=message.toCharArray();//转换为字符数组
int[]asciiArray=new int[charArray.length];//存储ascii码的数组
//将字符数组转换为整数数组的for循环
for(int count=0;count90 | | asciiArray[计数]<96 | | asciiArray[计数]>122){
继续;
}
其他的
asciiArray[count]=((asciiArray[count]-97)+移位)%26+97{
}
}//for循环结束
//将整型数组转换回字符数组的循环
for(int count=0;count
如果

编辑:


如果((asciiArray[count]<65 | | asciiArray[count]>90)和&(asciiArray[count]<96 | | | asciiArray[count]>122))
问题似乎在这一行:

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

你是说如果一个数字小于65,大于90,如果它小于96,大于122。。。您需要将这些和条件替换为或,因为现在,它的计算结果将始终为false

另外,另一种解决方法是,由于
charArray
包含字符值,您可以使用,因此您的代码变成:

if(Character.isLetter(charArray[count])){
asciiArray[count] = ((asciiArray[count]- 97)+ shift) % 26 + 97;
}

请注意,正如您的程序现在一样,您似乎在
else
块中有一个错位的
{

我将其更改为| |,但是加密根本不起作用。@user1768884:您需要更具体一些。另外,您可以尝试我的其他建议。