2024-11-25-蓝桥杯:数字转换字母

1.1k words

数字转换字母

问题与代码


思路

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
Scanner sc = new Scanner(System.in);
//获取第一个字符串
String S = sc.next();
//获取第二个字符串
String T = sc.next();
//因为字符串不能更改,所以用StringBuilder
StringBuilder sb = new StringBuilder();
for (int i = 0; i < T.length(); i++) {
char c = T.charAt(i);
if(c >= '0' && c <= '9'){
//创立默认值
char replacement = ' ';
switch(c){
case '0':
replacement = S.charAt(0);
break;
case '1':
replacement = S.charAt(1);
break;
case '2':
replacement = S.charAt(2);
break;
case '3':
replacement = S.charAt(3);
break;
case '4':
replacement = S.charAt(4);
break;
case '5':
replacement = S.charAt(5);
break;
case '6':
replacement = S.charAt(6);
break;
case '7':
replacement = S.charAt(7);
break;
case '8':
replacement = S.charAt(8);
break;
case '9':
replacement = S.charAt(9);
break;
default:
break;

}
sb.append(replacement);

}else {
sb.append(T.charAt(i));
}


}
String str = sb.toString();
System.out.println(str);


sc.close();