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

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);



        }

最佳答案

我将使用StringBuilder并在加密每个字符时附加它。简单高效的串联。 IE浏览器

StringBuilder sb = new StringBuilder();

for (char c : namech)
{
    // ... your code was here
    sb.append(d);
}

System.out.println(sb.toString());

10-02 07:36