原题:
[img]http://dl.iteye.com/upload/attachment/161372/c4058574-982a-334c-9b38-990377985180.jpg[/img]
(6)假设有一段程序对字符串进行加密,加密后的字符串的第一个字符是原字符串的最后一个字符,其余的每个字符串是对应的原字符串中的前一个字符的值加上3。比如“welcome”,末尾的字符为“e”,“welcom”依次加上 3 后成为“zhofrp”,故加密后的结果为“zhofrp”。程序由用户任意输入一个字符串,加密后输出。
我的解答思路很直接,如下:
这样写还不完善,各位javaeye好友有什么见解和应该改进的请尽管说吧:)
[img]http://dl.iteye.com/upload/attachment/161372/c4058574-982a-334c-9b38-990377985180.jpg[/img]
(6)假设有一段程序对字符串进行加密,加密后的字符串的第一个字符是原字符串的最后一个字符,其余的每个字符串是对应的原字符串中的前一个字符的值加上3。比如“welcome”,末尾的字符为“e”,“welcom”依次加上 3 后成为“zhofrp”,故加密后的结果为“zhofrp”。程序由用户任意输入一个字符串,加密后输出。
我的解答思路很直接,如下:
package com.magical;
import java.util.Scanner;
public class TestPsw {
public static void main(String[] args)
{
String password = new Scanner(System.in).nextLine();
int length = password.length();
password = password.substring(0, length-1);
char[] chars = new char[length-1];
for (int i=0; i<length-1; i++)
{
chars[i] = password.charAt(i);
chars[i] += 3;
}
System.out.println(new String(chars));
}
}
这样写还不完善,各位javaeye好友有什么见解和应该改进的请尽管说吧:)