java键盘循环录入_2019-01-28从键盘循环录入录入一个字符串,输入"end"表示结束

package homework;

import java.util.Scanner;

/*(1)从键盘循环录入录入一个字符串,输入"end"表示结束

(2)将字符串中大写字母变成小写字母,小写字母变成大写字母,其它字符用"*"代替,并统计字母的个数

举例:

键盘录入:Hello12345World

输出结果:hELLO*****wORLD

总共10个字母*/

public class Work3 {

public static void main(String[] args) {

String s = end();

System.out.println(s);

cast(s);

}

//利用stringbuffer的append和indexof功能,当没有索引的时候,indexof返回-1  实现功能(1)

public static String end() {

StringBuffer str = new StringBuffer();

while (true) {

String a = new Scanner(System.in).next();

str.append(a);

if (str.indexOf("end") >= 0) {break;}

}

// System.out.println(str);

return str.toString();

}

//遍历出字符串的每一个字符串,重新定义一个stringbuffer,每次都进行处理

public static void cast(String s) {

StringBuffer str = new StringBuffer(s);

int num = 0;

for (int i =0;i

if (str.charAt(i)>='a' && str.charAt(i)<='z') {

str = str.replace(i, i+1, str.substring(i, i+1).toUpperCase());

num++;

}

else if (str.charAt(i)>='A' && str.charAt(i)<='Z') {

str = str.replace(i, i+1, str.substring(i, i+1).toLowerCase());

num++;

}

else {

str = str.replace(i, i+1, "*");

}

}

/*{

if (str.charAt(i)>='a' && str.charAt(i)<='z') {

str.substring(i,1).toUpperCase();

num++;

}

else if (str.charAt(i)>='A' && str.charAt(i)<='Z') {

str.substring(i,1).toLowerCase();

num++;

}

else {

str.replace(i,1,"*");

}

}*/

System.out.println(str.toString());

System.out.println(num);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值