gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 00:06:59 25 4
gpt4 key购买 nike

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

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;
}

}

最佳答案

您可以使用正则表达式来处理这个问题,如下所示:

public static void main(String[] args) {
String str = "This string has FIVE uppercase characters within itself.";
System.out.println(str.replaceAll("[^\\p{javaUpperCase}]","").length());
}

输出:

5

这大致相当于以下内容:

public static void main(String[] args) {
String str = "This string has FIVE uppercase characters within itself.";
int uppercases = 0;
for(char c : str.toCharArray()) {
uppercases += Character.isUpperCase(c) ? 1 : 0;
}
System.out.println(uppercases);
}

输出:

5

现在,假设您在某种方法中具有此功能(您确实这样做了):

public static int sumLetter(String m) { ... }

您想要将字符串与大写长度相关联。制作一个简单的数据类:

final class StringWithUppercaseSize {
public final String string;
public final int uppercaseLength;

public StringWithUppercaseSize(String string, int uppercaseLength) {
this.string = string;
this.uppercaseLength = uppercaseLength;
}

public int getUppercaseLength() {
return this.uppercaseLength;
}
}

现在,您创建一个由这些对象组成的数组:

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);

输入:

Abc
abc
aBC

输出:

abc has a minimum number of uppercase: 0
aBC has a maximum number of uppercase: 2

这是我的完整测试代码:

Main.java

class Main {
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);
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);
}
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;
}
}

StringWithUppercaseSize.java

final class StringWithUppercaseSize {
public final String string;
public final int uppercaseLength;

public StringWithUppercaseSize(String string, int uppercaseLength) {
this.string = string;
this.uppercaseLength = uppercaseLength;
}

public int getUppercaseLength() {
return this.uppercaseLength;
}
}

最后,请注意,不要在您打算保留和维护的代码中执行此操作。有很多方法可以使事物更具可读性和可维护性。例如,您可以为数据类编写实际的 getter/setter。您可以将公共(public)数据成员设为私有(private)以保留信息隐藏。您可以将用户输入附加到列表中,然后使用 for-each 循环之类的东西来创建另一个大写大小的列表。您可以使用 for-each 循环遍历这两个列表,以创建 StringWithUppercaseSize 列表。您可能可以使用带有 Collectors.maxBy 的 Stream 来查找最大元素而不是排序等。

关于java - 如何输出与其大写长度相连的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58150116/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com