关于String Class,编写下面问题的代码

  1. Write a piece of code that converts a word with letters in random upper and lower case to the same word with the first and last letters in upper case and the other letters lower case.

e.g. CAtaStROPhe converts to CatastrophE.

Test your code by performing the conversion for several different input words.
翻译一下看就行

  1. You must use String format() and replace() methods to display the following shape of emojis:

img

import java.util.Random;

public class Years {
    public static final String LETTERCHAR = "abcdefghijkllmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    public static String generateString(int length) {
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        int len = LETTERCHAR.length();
        for (int i = 0; i < length; i++) {
            sb.append(LETTERCHAR.charAt(random.nextInt(len)));
        }
        return sb.toString();
    }

    public static void main(String[]args){
        int len = 10;
        String s = generateString(len);
        System.out.println(s);
        System.out.println(changeString(s));
        showEmoijs();
    }

    // Write a piece of code that converts a word with letters in random upper and lower case
    // to the same word with the first and last letters in upper case and the other letters lower case.
    public static String changeString(String str){
        char[] tmp = str.toLowerCase().toCharArray();
        int len = str.length();
        tmp[0] = (char) (tmp[0]-32);
        tmp[len-1] = (char) (tmp[len-1]-32);
        return String.valueOf(tmp);
    }
    // 第二个emoij不知道咋找,感觉是咖啡,给你找个查不多的
    public static void showEmoijs(){
        String a = "\uD83D\uDC49";
        String b = "☕";
        StringBuffer sb = new StringBuffer();
        for(int i=0;i<5;i++)
           sb.append(a);
        String init = sb.toString();
        String res;
        int len = a.length();
        for(int i=0;i<4;i++){
            res = init.substring((4-i) *len);
            res = res.replace(a,b);
            System.out.format("%s%s\n",init.substring(0, (4-i) *len) , res);
        }
    }
}

img