Java编程之大小写字母转换

您所在的位置:网站首页 abcdefg的大写字母 Java编程之大小写字母转换

Java编程之大小写字母转换

2023-12-16 03:06| 来源: 网络整理| 查看: 265

一、题目 1、程序一:从键盘输入一个字符串,将其中的小写字母全部转换为大写字母; 2、程序额二:从键盘输入一个字符串,将其中的小大写字母全部转换为小写字母。

二、实验代码 1、小写字母->大写字母

package fighting; import java.util.Scanner; public class fighting { public static void main(String[] args) { System.out.println("输入的字符串为:"); Scanner sc=new Scanner(System.in); String str=sc.next(); String str1=""; for(int i=0;i ch=(char)((int)ch-32);//先将字符ch转换为整型计算ASCII码,再转换为字符型输出字符A~Z的ASCII码为65~90,字符a~z的ASCII码为97~122,每个大小写字符的ASCII码都相差32,因此用小写字符的ASCII码 减去32即可得到该字符的大写形式 } str1+=ch; } System.out.println("方法一输出的字符串为:"); System.out.println(str1); System.out.println("方法二输出的字符串为:"); System.out.println(str.toUpperCase());//toUpperCase()将字母转换为大写 } }

2、大写字母->小写字母

package fighting; import java.util.Scanner; public class fighting { public static void main(String[] args) { System.out.println("输入的字符串为:"); Scanner sc=new Scanner(System.in); String str=sc.next(); String str1=""; for(int i=0;i ch=(char)((int)ch+32);//先将字符ch转换为整型计算ASCII码,再转换为字符型输出字符A~Z的ASCII码为65~90,字符a~z的ASCII码为97~122,每个大小写字符的ASCII码都相差32,因此用大写字符的ASCII码 加上32即可得到该字符的小写形式 } str1+=ch; } System.out.println("方法一输出的字符串为:"); System.out.println(str1); System.out.println("方法二输出的字符串为:"); System.out.println(str.toLowerCase());//toLowerCase()将字母转换为小写 } }

三、运行结果 1、小写字母->大写字母 在这里插入图片描述 2、大写字母->小写字母 在这里插入图片描述



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3