gpt4 book ai didi

Java程序处理非字母字符

转载 作者:行者123 更新时间:2023-12-02 07:29:57 25 4
gpt4 key购买 nike

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

最佳答案

更改 if 内的条件

编辑:

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

关于Java程序处理非字母字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13043253/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com