如何实现字符串的墨西哥波浪式大小写转换?代码输出异常求助
Hey there! Let's break down why your current code isn't producing the Mexican wave effect you're aiming for, and how to fix it.
The Problem with Your Current Code
Looking at your loop:
for(int i=0;i<s.length();i++) { a1=s.charAt(i); a2=Character.toUpperCase(a1); System.out.println(s); }
You're correctly grabbing each character and converting it to uppercase with Character.toUpperCase(), but here's the issue: you never actually replace the original character in the string s with the uppercase version. Instead, you just print the original string every time—so you'll always get the same lowercase output five times for "hello".
Another key thing to remember: in Java, String objects are immutable—you can't change individual characters directly once the string is created. You need to create a new string (or use a mutable alternative like StringBuilder) to hold the modified version.
Solution 1: Using StringBuilder (Efficient and Clean)
This approach uses StringBuilder to easily modify specific characters, then builds up your final wave output:
public class MexicanWave { public static void main(String[] args) { String input = "hello"; // First convert the entire string to lowercase to handle mixed-case inputs String lowerInput = input.toLowerCase(); StringBuilder waveResult = new StringBuilder(); for (int i = 0; i < lowerInput.length(); i++) { // Create a mutable StringBuilder from the lowercase string StringBuilder currentWave = new StringBuilder(lowerInput); // Convert the i-th character to uppercase currentWave.setCharAt(i, Character.toUpperCase(currentWave.charAt(i))); // Add to result, with a space separator (except for the first entry) if (i != 0) { waveResult.append(" "); } waveResult.append(currentWave); } // Print the final wave output System.out.println(waveResult.toString()); } }
Key Details:
toLowerCase(): Ensures the base string is all lowercase, so even if your input has random uppercase letters, the wave effect stays consistent.StringBuilder.setCharAt(): Lets you modify a specific character in the mutable string builder—this is how you actually apply the uppercase to the correct position.Character.toUpperCase(): Works exactly as you expected, but now we're using the converted character by putting it back into the string builder.
Solution 2: Using a Char Array
If you prefer working with arrays, this approach converts the string to a char array, modifies the target character, then converts it back to a string:
public class MexicanWave { public static void main(String[] args) { String input = "hello"; String lowerInput = input.toLowerCase(); String[] waveParts = new String[lowerInput.length()]; for (int i = 0; i < lowerInput.length(); i++) { char[] charArray = lowerInput.toCharArray(); // Convert the i-th character to uppercase charArray[i] = Character.toUpperCase(charArray[i]); // Store the modified string in the array waveParts[i] = new String(charArray); } // Join all parts with spaces and print System.out.println(String.join(" ", waveParts)); } }
This works because char arrays are mutable—you can directly change the value at index i, then create a new String from the modified array.
Either of these solutions will output exactly what you want for "hello":
Hello hEllo heLlo helLo hellO
内容的提问来源于stack exchange,提问作者Alex

