gpt4 book ai didi

Java:将一组字符转换为单个字符串

转载 作者:行者123 更新时间:2023-12-01 18:50:03 24 4
gpt4 key购买 nike

我一直在尝试制作一个程序,允许我加密用户名和密码并打印结果。我可以让它工作,但我想将循环中出现的字符排序到单个字符串中,以使其更易于管理。有什么建议吗?

System.out.println("\nYour encrypted username is " );

for(char c : namech)
{
int temp = (int)c;
int namecode = 0;

//makes the characters w,x,y,z loops back to the start of the alphabet
if(temp<=90 & temp>=65)
{
namecode = temp+4;

if(namecode == 91)
{
namecode = 65;
}
else if(namecode == 92)
{
namecode = 66;
}
else if(namecode == 93)
{
namecode = 67;
}
else if(namecode == 94)
{
namecode = 68;
}

int i = namecode;

char d = (char)i;


System.out.print(d);



}

最佳答案

I want to sort the characters that come out of the loop into a single String

最简单的方法可能是将整个内容转换为 char[],然后将其转换回字符串:

char[] chars = namech.toCharArray();
for (int i = 0; i < chars.length(); i++) {
chars[i] = obfuscateCharacter(chars[i]);
}
String obfuscated = new String(chars);

... 其中 obfuscateCharacter 基本上是当前循环的主体,但提取到处理单个字符的方法中,这更容易测试等。(我拒绝将其称为加密字符,因为这给人一种安全的印象:)

关于Java:将一组字符转换为单个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16177417/

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