如何输出与其大写长度相连的字符串

在这个程序中,我试图创建一个方法来获取字符串来计算大写字母的数量。在程序结束时,我想显示与其字符串关联的最小数量以及与其字符串关联的最大数量,这就是我遇到麻烦的地方。有没有办法以某种方式将它们连接或关联在一起?到目前为止,这是我的代码:


import java.util.Scanner;

import java.util.Arrays;

public class test {

    public static void main(String[] args) {

        System.out.println("Please input a string:");

        Scanner input = new Scanner(System.in);

        String s1 = input.nextLine();

        String s2 = input.nextLine();

        String s3 = input.nextLine();

        int i1 = sumLetter(s1);

        int i2 = sumLetter(s2);

        int i3 = sumLetter(s3);



        int[] array = new int[3];

        array[0] = i1;

        array[1] = i2;

        array[2] = i3;

        Arrays.sort(array);


        System.out.println(s1 + " has a maximum number of uppercase: "+ array[2]);

        System.out.println(s3 + " has a maximum number of uppercase: "+ array[0]);



    }

    public static int sumLetter(String m) {

        int count = 0;

        for(int i = 0; i < m.length();i++) {

            if(Character.isUpperCase(m.charAt(i)))

                count++;

        }

        return count;

    }


    }


临摹微笑
浏览 64回答 1
1回答

慕码人2483693

您可以使用正则表达式来处理这个问题,如下所示:public static void main(String[] args) {&nbsp; String str = "This string has FIVE uppercase characters within itself.";&nbsp; System.out.println(str.replaceAll("[^\\p{javaUpperCase}]","").length());}输出:5这大致相当于以下内容:public static void main(String[] args) {&nbsp; String str = "This string has FIVE uppercase characters within itself.";&nbsp; int uppercases = 0;&nbsp; for(char c : str.toCharArray()) {&nbsp; &nbsp; uppercases += Character.isUpperCase(c) ? 1 : 0;&nbsp; }&nbsp; System.out.println(uppercases);}输出:5现在,假设您在某种方法中具有此功能(您确实这样做了):public static int sumLetter(String m) { ... }您想要将字符串与大写长度相关联。制作一个简单的数据类:final class StringWithUppercaseSize {&nbsp; public final String string;&nbsp; public final int uppercaseLength;&nbsp; public StringWithUppercaseSize(String string, int uppercaseLength) {&nbsp; &nbsp; this.string = string;&nbsp; &nbsp; this.uppercaseLength = uppercaseLength;&nbsp; }&nbsp; public int getUppercaseLength() {&nbsp; &nbsp; return this.uppercaseLength;&nbsp; }}现在,您创建一个由这些对象组成的数组:Scanner input = new Scanner(System.in);String s1 = input.nextLine();String s2 = input.nextLine();String s3 = input.nextLine();int i1 = sumLetter(s1);int i2 = sumLetter(s2);int i3 = sumLetter(s3);StringWithUppercaseSize[] sizes = { new StringWithUppercaseSize(s1, i1), new StringWithUppercaseSize(s2, i2), new StringWithUppercaseSize(s3, i3) };按大写大小对数组进行排序:Arrays.sort(sizes, Comparator.comparing(StringWithUppercaseSize::getUppercaseLength));输出最小/最大大写字符串/长度:System.out.println(sizes[0].string + " has a minimum number of uppercase: "+ sizes[0].uppercaseLength);System.out.println(sizes[2].string + " has a maximum number of uppercase: "+ sizes[2].uppercaseLength);输入:AbcabcaBC输出:abc has a minimum number of uppercase: 0aBC has a maximum number of uppercase: 2这是我的完整测试代码:Main.javaclass Main {public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Please input a string:");&nbsp; &nbsp; &nbsp; &nbsp; Scanner input = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; String s1 = input.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; String s2 = input.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; String s3 = input.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; int i1 = sumLetter(s1);&nbsp; &nbsp; &nbsp; &nbsp; int i2 = sumLetter(s2);&nbsp; &nbsp; &nbsp; &nbsp; int i3 = sumLetter(s3);&nbsp; &nbsp; &nbsp; &nbsp; StringWithUppercaseSize[] sizes = { new StringWithUppercaseSize(s1, i1), new StringWithUppercaseSize(s2, i2), new StringWithUppercaseSize(s3, i3) };&nbsp; &nbsp; &nbsp; &nbsp; Arrays.sort(sizes, Comparator.comparing(StringWithUppercaseSize::getUppercaseLength));&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(sizes[0].string + " has a minimum number of uppercase: "+ sizes[0].uppercaseLength);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(sizes[2].string + " has a maximum number of uppercase: "+ sizes[2].uppercaseLength);&nbsp; &nbsp; }&nbsp; &nbsp; public static int sumLetter(String m) {&nbsp; &nbsp; &nbsp; &nbsp; int count = 0;&nbsp; &nbsp; &nbsp; &nbsp; for(int i = 0; i < m.length();i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(Character.isUpperCase(m.charAt(i)))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return count;&nbsp; &nbsp; }}StringWithUppercaseSize.javafinal class StringWithUppercaseSize {&nbsp; public final String string;&nbsp; public final int uppercaseLength;&nbsp; public StringWithUppercaseSize(String string, int uppercaseLength) {&nbsp; &nbsp; this.string = string;&nbsp; &nbsp; this.uppercaseLength = uppercaseLength;&nbsp; }&nbsp; public int getUppercaseLength() {&nbsp; &nbsp; return this.uppercaseLength;&nbsp; }}最后,请注意,不要在您打算保留和维护的代码中执行此操作。有很多方法可以使事物更具可读性和可维护性。例如,您可以为数据类编写实际的 getter/setter。您可以将公共数据成员设为私有以保留信息隐藏。您可以将用户输入附加到列表中,然后使用 for-each 循环之类的东西来创建另一个大写大小的列表。您可以使用 for-each 循环遍历这两个列表以创建一个 List StringWithUppercaseSize。您可能可以使用带有 Collectors.maxBy 的 Stream 来查找最大元素而不是排序等。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java