在这个程序中,我试图创建一个具有字符串计算大写字母数量的方法。在程序的最后,我想显示与它的字符串关联的最小数量和与它的字符串关联的最大数量,这是我遇到麻烦的地方。有没有办法将它们以某种方式连接或关联在一起?到目前为止,这是我的代码:
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;
}
}
答案 0 :(得分:0)
您可以使用正则表达式来处理此问题,如下所示:
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) { ... }
您想要将String与大写长度关联。制作一个简单的数据类:
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;
}
}
最后,请注意,请勿在您打算保留和维护的代码中执行此操作。有很多方法可以使事情更具可读性和可维护性。例如,您可以为数据类编写实际的获取器/设置器。您可以将公共数据成员设为私有,以隐藏信息。您可以将用户输入附加到列表中,然后使用for-each循环之类的东西来创建另一个大写大小的列表。您可以使用for-each循环遍历这两个列表,以创建StringWithUppercaseSize
的列表。您可能可以将Stream与Collectors.maxBy一起使用以查找最大元素,而不是进行排序等。