- String.toCharArray()将字符串转为字符数组
- A的ASCII码是65,a的ASCII码是97。
同样的字母,小写字母的ASCII码值比大写字母的ASCII码值大32,小写字母ASCII = 大写字母ASCII + 32 。 - 增强型遍历for (char c : ch)
ASCII码记不住的版本:
import java.util.*; import java.lang.*; public class Main { public static void main (String[] args) { Scanner sc = new Scanner(System.in); String str = sc.next(); char[] ch = str.toCharArray(); for (int i = 0; i <ch.length; i++) { if (ch[i] >= 'a' && ch[i] <= 'c') { ch[i] = '2'; } else if (ch[i]>='d' && ch[i] <= 'f') { ch[i] = '3'; } else if (ch[i]>='g' && ch[i] <= 'i') { ch[i] = '4'; } else if (ch[i]>='j' && ch[i] <= 'l') { ch[i] = '5'; }else if (ch[i]>='m' && ch[i] <= 'o') { ch[i] = '6'; }else if (ch[i]>='p' && ch[i] <= 's') { ch[i] = '7'; }else if (ch[i]>='t' && ch[i] <= 'v') { ch[i] = '8'; }else if (ch[i]>='w' &&ch[i] <= 'z') { ch[i] = '9'; } if (ch[i]>='A' && ch[i]<='Z') { if (ch[i] != 'Z') { ch[i]++; ch[i] += 32; } else { ch[i] = 'A'; ch[i] += 32; } } } for (char c : ch) { System.out.print(c); } } }