(1)从键盘循环录入录入一个字符串,输入"end"表示结束
(2)将字符串中大写字母变成小写字母,小写字母变成大写字母,其它字符用"*"代替,并统计字母的个数
举例:
键盘录入:Hello12345World
输出结果:
修改后的字符串:hELLO*****wORLD
总共10个字母
修改后的字符串大写字母数:8个,小写字母数:2个,其他字符:5个
import java.util.Scanner;
public class StringExcise {
public static void main(String[] args) {
//用StringBuffer和StringBilder功能差不多,看个人喜好
StringBuilder sb = new StringBuilder();
@SuppressWarnings("resource")
String s = new Scanner(System.in).next();
do{
sb.append(s);
}while(sb.toString().equals("end"));//判断字符串s里有没有包含end数组
// do{
// sb.append(s);
//
// }while(sb.toString().indexOf("end")<0);//判断字符串s里有没有包含end数组
sb.delete(s.length()-"end".length(),s.length());//删除字符串s里的“end”
function( sb);
}
public static void function(StringBuilder sb) {
int lowNum = 0, upNum = 0,otherNum = 0;
for(int i=0 ; i<sb.length() ; i++) {
if(sb.charAt(i)>='A' && sb.charAt(i)<='Z') {
sb = sb.replace(i, i+1, sb.substring(i,i+1).toLowerCase());//将大写字母换小写
lowNum++;
}
else if(sb.charAt(i)>='a' && sb.charAt(i)<='z') {
sb = sb.replace(i, i+1, sb.substring(i,i+1).toUpperCase());//将小写字母换大写
upNum++;
}
else {
sb = sb.replace(i, i+1, "*");//将数字换成*
otherNum++;
}
}
System.out.println("修改后的字符串:"+sb.toString()+"\n总共"+(lowNum+upNum)+"个字母"
+"\n修改后的字符串大写字母数:"+upNum+"个,小写字母数:"+lowNum+"个,其他字符:"+otherNum+"个");
}
}
输出结果: