刚刚学java不会,请大神帮忙解答

ufheasuhewauhjdfuiewhufujqWU8OIU9OQEOIGUE9IFFOPTEO9FQIFO9WR0WSQFFEFTEAQFRESFGAEFED

4个回答

    public static void main(String[] args) {
        String str = "It's the first day of class, and the room is buzzing with excitement and expectation,curiosity and uncertainty";
        String[] strArrs = str.split(" ");
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        for (int i = 0; i < strArrs.length; i++) {
            if (map.get(strArrs[i]) != null) {
                Integer integer = map.get(strArrs[i]);
                integer++;
                map.put(strArrs[i], integer);
            } else {
                map.put(strArrs[i], 1);
            }
        }

        int maxNum = 0;
        String maxValue = null;
        for (int i = 0; i < strArrs.length; i++) {
            int tmp = map.get(strArrs[i]);
            if(tmp > maxNum) {
                maxNum = tmp;
                maxValue = strArrs[i];
            }
        }

        System.out.println(maxValue + " : " + maxNum);
    }

lbcab
lbcab 回复java1996: 代码给你贴下面了
4 年多之前 回复
java1996
java1996 回复java1996: 答案给了It's : 1应该是and3 怎么改
4 年多之前 回复
java1996
java1996 回复Grindstone_Fos: System.out.println("请输入英文句子:");
4 年多之前 回复
Grindstone_Fos
Grindstone_Fos 回复java1996: 改成 Scanner sc=new Scanner(System.in); String str=sc.next();
4 年多之前 回复
Grindstone_Fos
Grindstone_Fos 回复java1996: String str = "It's the first day of class, and the room is buzzing with excitement and expectation,curiosity and uncertainty";
4 年多之前 回复
lbcab
lbcab 回复java1996: 你不会连怎么获取终端输入都不会吧? 获取终端输入的字符串赋值给str不就行了
4 年多之前 回复
java1996
java1996 它这个是编写随机输入的一段英语,不是就那一句
4 年多之前 回复
    String msg = "It's the first day of class, and the room is buzzing with excitement and expectation,curiosity and uncertainty.";
    String regex = "(\\,|\\.)";
    msg = msg.replaceAll(regex, " ");
    String[] arr = msg.split(" ");
    Map<String,Integer> map = new HashMap<String,Integer>();
    for (int i = 0; i < arr.length; i++) {
        if (!map.containsKey(arr[i])) {
            map.put(arr[i], 1);
        }else{
            int count = map.get(arr[i]) + 1;
            map.put(arr[i], count);
        }
    }
    System.out.println(map);

这个问题主要考的是字符串对正则表达式的支持

public static void main(String[] args) {
        // "It's the first day of class, and the room is buzzing with excitement and expectation,curiosity and uncertainty"
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入字符串:");
        String str = sc.nextLine();
        String[] strArrs = str.split(" ");
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        for (int i = 0; i < strArrs.length; i++) {
            if (map.get(strArrs[i]) != null) {
                Integer integer = map.get(strArrs[i]);
                integer++;
                map.put(strArrs[i], integer);
            } else {
                map.put(strArrs[i], 1);
            }
        }

        int maxNum = 0;
        String maxValue = null;
        for (int i = 0; i < strArrs.length; i++) {
            int tmp = map.get(strArrs[i]);
            if (tmp > maxNum) {
                maxNum = tmp;
                maxValue = strArrs[i];
            }
        }

        System.out.println(maxValue + " : " + maxNum);
    }

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐