1 回答
TA贡献1860条经验 获得超9个赞
您可以使用正则表达式来处理这个问题,如下所示:
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。您可以将公共数据成员设为私有以保留信息隐藏。您可以将用户输入附加到列表中,然后使用 for-each 循环之类的东西来创建另一个大写大小的列表。您可以使用 for-each 循环遍历这两个列表以创建一个 List StringWithUppercaseSize。您可能可以使用带有 Collectors.maxBy 的 Stream 来查找最大元素而不是排序等。
添加回答
举报