HJ29 字符串加解密
大写字母的 ASCII 码值范围是 65 到 90,而小写字母的 ASCII 码值范围是 97 到 122。
然看清题目要求
输入描述:
第一行输入一串要加密的密码
第二行输入一串加过密的密码
输出描述:
第一行输出加密后的字符
第二行输出解密后的字符
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
//输出加密
String str = in.nextLine();
StringBuilder sb = new StringBuilder();
for (char c : str.toCharArray()) {
if ( c >= 'a' && c <= 'z') {
if (c == 'z') {
sb.append('A');
} else {
sb.append((char)(c - 31));
}
} else if (c >= 'A' && c <= 'Z') {
if (c == 'Z') {
sb.append('a');
} else {
sb.append((char)(c + 33));
}
} else if (c >= '0' && c <= '9') {
if (c == '9') {
sb.append('0');
} else {
sb.append((char)(c + 1));
}
}
}
System.out.println(sb);
//输出解密
String str1 = in.nextLine();
for (char c : str1.toCharArray()) {
if ( c >= 'a' && c <= 'z') {
if (c == 'a') {
System.out.print('Z');
} else {
System.out.print((char)(c-33));
}
} else if (c >= 'A' && c <= 'Z') {
if (c == 'A') {
System.out.print('z');
} else {
System.out.print((char)(c+31));
}
} else if (c >= '0' && c <= '9') {
if (c == '0') {
System.out.print('9');
} else {
System.out.print((char)(c - 1));
}
}
}
}
}
HJ31 单词倒排
读取字符将非法字符转换为空格,根据空格分割字符字符串,存储到字符串数组中,倒序输出字符串数组
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
String str = in.nextLine();
StringBuilder sb = new StringBuilder();
char[] chars = str.toCharArray();
for (int i = 0; i < chars.length; i++) {
if (Character.isLetter(chars[i])) {
sb.append(chars[i]);
} else {
sb.append(' ');
}
}
String[] arr = sb.toString().split(" ");
for (int i = arr.length - 1; i >= 0; i--) {
System.out.print(arr[i] + " ");
}
}
}
}