- package com.itheima;
- import java.util.Scanner;
- import javax.print.attribute.standard.Sides;
- /**
- * 编写程序,从键盘接收一个字符串,对字符串中的字母进行大小写互转(大写字母转成小写,小写字母转成大写)
- *
- * @author zyy
- *
- */
- public class Test7 {
- public static void main(String[] args) {
- //获取字符数组
- String str = getString();
- //数组字母大小写转换
- char[] strArray = str.toCharArray();
- reserveCase(strArray);
- //输出数组
- System.out.println(new String(strArray));
- }
- /**
- * 大小写转换,用ASICC来操作,感觉是最简单的,也可以用正则表达式
- * @param strArray
- */
- private static void reserveCase(char[] strArray) {
-
- int dist = 'a'-'A';
- for (int i = 0; i < strArray.length; i++) {
- if('a'<=strArray[i]&&strArray[i]<='z'){
- //小写转大写
- strArray[i] = (char) (strArray[i] - dist);
- }else if('A'<=strArray[i]&&strArray[i]<='Z'){
- //大写转小写
- strArray[i] = (char) (strArray[i] + dist);
- }
- }
- }
- /**
- * 获取一个字符串
- *
- * @return 返回字符串
- */
- private static String getString() {
- return new Scanner(System.in).next();
- }
- }
复制代码
|
|