gpt4 book ai didi

java - 使用凯撒密码进行加密

转载 作者:行者123 更新时间:2023-12-01 12:13:16 25 4
gpt4 key购买 nike

是否有办法更改此代码,使其仅加密大写字母?我不需要它做小写或符号。谢谢!

public class CaesarCypher
{
public static final int MOVE_DOWN = 4;
public static void main(String [] args)
{

String plainText;
char character;

System.out.println("Enter sentence or word to Encrypt: ");
plainText = Console.readString();

for ( int iteration = 0 ; iteration < plainText.length() ; iteration++ )
{
character = plainText.charAt( iteration );
if ( character != ' ' )
{
character = (char) ( 'a' + ( character - 'a' + MOVE_DOWN ) %26 );
}

System.out.print(character);
}


}
}

最佳答案

只需将非空格测试替换为大写字母测试,将“a”替换为“A”即可。

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class CaesarCypher {

public static final int MOVE_DOWN = 4;
public static void main(String [] args)
{

String plainText = "";
char character;

System.out.println("Enter sentence or word to Encrypt: ");
InputStreamReader converter = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(converter);
try{
plainText = in.readLine();

for ( int iteration = 0 ; iteration < plainText.length() ; iteration++ )
{
character = plainText.charAt( iteration );
if ( character >='A' && character <= 'Z' )
{
character = (char) ( 'A' + ( character - 'A' + MOVE_DOWN ) %26 );
}

System.out.print(character);
}
}
catch (Exception e){
System.err.println(e.getMessage());
}

}
}

关于java - 使用凯撒密码进行加密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27160578/

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