美文网首页
2019-01-28从键盘循环录入录入一个字符串,输入"end"

2019-01-28从键盘循环录入录入一个字符串,输入"end"

作者: 全村的希望_5461 | 来源:发表于2019-01-29 10:23 被阅读0次

    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<str.length();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);

    }

    }

    相关文章

      网友评论

          本文标题:2019-01-28从键盘循环录入录入一个字符串,输入"end"

          本文链接:https://www.haomeiwen.com/subject/tyzojqtx.html