Java开发:如何关联字符串与大写字母数并输出对应最值字符串?
Got it, the core problem here is that once you extract the uppercase counts into an array and sort it, you lose the connection between each count and the original string. Your current code assumes s1 has the maximum count and s3 has the minimum, which won't hold true unless the input strings happen to be in that order. Let's fix this with two straightforward approaches that keep the string and its count linked.
Approach 1: Create a Custom Class to Bind String and Count
This is the most readable and scalable approach, especially if you might need to add more properties later. We'll define a simple class to store each string and its corresponding uppercase letter count, then sort a list of these objects.
First, add the custom class (you can put it inside your test class or as a separate class):
class StringCountPair { private String string; private int uppercaseCount; // Constructor public StringCountPair(String string, int uppercaseCount) { this.string = string; this.uppercaseCount = uppercaseCount; } // Getters to access the values public String getString() { return string; } public int getUppercaseCount() { return uppercaseCount; } }
Now modify your main method to use this class:
import java.util.Scanner; import java.util.Arrays; import java.util.List; import java.util.Collections; import java.util.Comparator; 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(); // Create pairs linking each string to its uppercase count StringCountPair pair1 = new StringCountPair(s1, sumLetter(s1)); StringCountPair pair2 = new StringCountPair(s2, sumLetter(s2)); StringCountPair pair3 = new StringCountPair(s3, sumLetter(s3)); List<StringCountPair> stringCountPairs = Arrays.asList(pair1, pair2, pair3); // Sort the list by uppercase count (ascending order) Collections.sort(stringCountPairs, Comparator.comparingInt(StringCountPair::getUppercaseCount)); // Retrieve the min and max pairs StringCountPair minPair = stringCountPairs.get(0); StringCountPair maxPair = stringCountPairs.get(stringCountPairs.size() - 1); // Output the results correctly System.out.println(maxPair.getString() + " has the maximum number of uppercase letters: " + maxPair.getUppercaseCount()); System.out.println(minPair.getString() + " has the minimum number of uppercase letters: " + minPair.getUppercaseCount()); } 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; } }
Approach 2: Use Map Entries (No Custom Class Needed)
If you don't want to create a new class, you can use Java's built-in Map.Entry to pair strings with their counts. AbstractMap.SimpleEntry is a convenient implementation for this.
Here's how to adjust your main method:
import java.util.Scanner; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.AbstractMap; 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(); // Create a list of entries where key = string, value = uppercase count List<Map.Entry<String, Integer>> stringCountEntries = new ArrayList<>(); stringCountEntries.add(new AbstractMap.SimpleEntry<>(s1, sumLetter(s1))); stringCountEntries.add(new AbstractMap.SimpleEntry<>(s2, sumLetter(s2))); stringCountEntries.add(new AbstractMap.SimpleEntry<>(s3, sumLetter(s3))); // Sort the list by the value (uppercase count) stringCountEntries.sort(Map.Entry.comparingByValue()); // Get min and max entries Map.Entry<String, Integer> minEntry = stringCountEntries.get(0); Map.Entry<String, Integer> maxEntry = stringCountEntries.get(stringCountEntries.size() - 1); // Print the correct results System.out.println(maxEntry.getKey() + " has the maximum number of uppercase letters: " + maxEntry.getValue()); System.out.println(minEntry.getKey() + " has the minimum number of uppercase letters: " + minEntry.getValue()); } 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; } }
Why Your Original Code Failed
When you sorted the array of counts, you broke the link between each count and the string it came from. For example, if s2 had the highest count, array[2] would hold that count, but your code still printed s1 as the maximum—this is the root of the issue. By keeping the string and count paired together, sorting preserves the relationship, so you always know which string corresponds to which count.
内容的提问来源于stack exchange,提问作者Exrial

