题目描述
要将"China"译成密码,译码规律是:用原来字母后面的第4个字母代替原来的字母.
例如,字母"A"后面第4个字母是"E"."E"代替"A"。因此,"China"应译为"Glmre"。
请编一程序,用赋初值的方法使cl、c2、c3、c4、c5五个变量的值分别为,’C’、’h’、’i’、’n’、’a’,经过运算,使c1、c2、c3、c4、c5分别变为’G’、’l’、’m’、’r’、’e’,并输出。
输入格式
China
输出格式
加密后的China
样例输入
China
样例输出
Glmre
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String str1 = sc.nextLine(); //向字符串中输入
int str1Length = str1.length(); //调用String类的length()方法,得到str1的字符串长度
char[] ch = new char[str1Length]; //定义char型数组
str1.getChars(0, str1Length, ch, 0); //调用String类的getChars()方法,将字符串复制到字符数组内
for(int i = 0 ; i < ch.length ; i++)
ch[i] = (char)(ch[i] + 4);
System.out.println(ch);
}
}