A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© xiong910627 中级黑马   /  2014-9-3 21:10  /  2680 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 xiong910627 于 2014-9-5 12:24 编辑

编写程序,从键盘接收一个字符串,对字符串中的字母进行大小写互转(大写字母转成小写,小写字母转成大写)。

public class Test5 {

     public static void main(String[] args) {
            
            System.out.println("字母大小写互换,请输入后回车");   
        String arry;
        String result="";
        //获取键盘输入的字符串
        Scanner sc = new Scanner(System.in);

        arry=sc.nextLine();
        //用正则表达式设定字符串范围
        String regx1="[a-z]";
        String regx2="[A-Z]";
        System.out.println("转换如下");

        for(int i=0;i<arry.length();i++)
        {
                String sub=arry.substring(i, i+1);
                //判断是否是小写字母,是则转换为大写
                if(sub.matches(regx1))
                {
                        sub=sub.toUpperCase();
                        result+=sub;
                }
                //判断是否是小写字母,是则转换为小写
                else if(sub.matches(regx2))
                {
                        sub=sub.toLowerCase();
                        result+=sub;
                }
                //其他类型则直接输出
                else
                {
                        result+=sub;
                }
        }
        System.out.println(result);
        }
}

5 个回复

倒序浏览
用了正则表达式,也可以直接用char值来改吧
回复 使用道具 举报
有代码吗?
回复 使用道具 举报
看看。。
回复 使用道具 举报
根据上面的代码改了一小下:用字符数组改的

  1. package lm.test;

  2. import java.util.Scanner;

  3. public class ExchangeULCaseLetter {

  4.      public static void main(String[] args) {
  5.            
  6. /*            System.out.println("字母大小写互换,请输入后回车");   
  7.         String arry;
  8.         String result="";
  9.         //获取键盘输入的字符串
  10.         Scanner sc = new Scanner(System.in);

  11.         arry=sc.nextLine();
  12.         //用正则表达式设定字符串范围
  13.         String regx1="[a-z]";
  14.         String regx2="[A-Z]";
  15.         System.out.println("转换如下");

  16.         for(int i=0;i<arry.length();i++)
  17.         {
  18.                 String sub=arry.substring(i, i+1);
  19.                 //判断是否是小写字母,是则转换为大写
  20.                 if(sub.matches(regx1))
  21.                 {
  22.                         sub=sub.toUpperCase();
  23.                         result+=sub;
  24.                 }
  25.                 //判断是否是小写字母,是则转换为小写
  26.                 else if(sub.matches(regx2))
  27.                 {
  28.                         sub=sub.toLowerCase();
  29.                         result+=sub;
  30.                 }
  31.                 //其他类型则直接输出
  32.                 else
  33.                 {
  34.                         result+=sub;
  35.                 }
  36.         }
  37.         System.out.println(result);
  38.         */
  39.              
  40.              
  41.              System.out.println("字母大小写互换,请输入后回车");  
  42.              Scanner scanner = new Scanner(System.in);
  43.              String src = scanner.nextLine();
  44.              // 把数据放到一个字符数组  2deHJkd
  45.              char[] chs = src.toCharArray();
  46.              //遍历数组,把字母大小写换掉
  47.              String result = "";
  48.              for(int i = 0; i < chs.length; i++)
  49.              {
  50.                      //将字符转成字符串
  51.                      String temp = Character.valueOf(chs[i]).toString();
  52.                      
  53.                      if(temp.matches("[A-Z]"))  //如果是大写,转成小写, 装入字符串
  54.                              result += temp.toLowerCase();
  55.                      if(temp.matches("[a-z]"))
  56.                              result += temp.toUpperCase(); //如果是小写,转成大写, 装入字符串
  57.                      else
  58.                              result += temp;  // 其他字符,原样装入字符串。
  59.              }
  60.              
  61.              System.out.println(result);
  62.              
  63.         }
  64.              
  65. }
复制代码
回复 使用道具 举报
不用这么复杂,字母大小写unicode相差32,把小写字母减32再转char就是大写
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马