将字符串“I am a college student at CCBUPT”中的大写字
母改小写字母,小写字母改为大写字母,打印输出,并显示程序运行时间(毫秒)。
public class UpLowCaseTest {public static void main(String[] args) {long start,end;start=System.nanoTime();//开始计时StringBuffer s1=new StringBuffer();String s2="abcde fgeh IJKLM";//创建一个字符串char c[]=s2.toCharArray();//把字符串转换为字符串数组for(int i=0;i<s2.length();i++){if(c[i]>='a')s1.append((c[i]+"").toUpperCase());//如果是小写转换为大写elses1.append((c[i]+"").toLowerCase());//如果是大写转换为小写}System.out.println(s1);//输出结果end=System.nanoTime();//结束计时System.out.println("执行" + s2.length() + "次循环需要的时间");//计算执行循环的次数System.out.println("需要的时间是"+(end-start)+"纳秒");//输出时间}}
运行结果
将字符串“I am a college student at CCBUPT”中的大写字 母改小写字母 小写字母改为大写字母 打印输出 并显示程序运行时 间(毫秒)。