gpt4 book ai didi

java - 用 Java 数组中的特殊字符替换用户输入中的元音

转载 作者:太空宇宙 更新时间:2023-11-04 11:08:23 25 4
gpt4 key购买 nike

Stack Overflow 用户您好,我正在尝试用不同的字符替换用户输入中的元音。下面的第一种方法通过用指定的特殊字符替换每个元音来增强作为字符串参数传递的密码。我遇到的唯一问题是在主打印时,我需要将输入的密码替换为元音。例如,如果输入“hello”,则应打印“h3ll0”。第二种方法的 return 语句与此有关,但我不确定。如果有人可以提供任何建议,我们将不胜感激。

 public static String enhancePassword(String oldPassword)
{
String vowel []= {"a","e","i","o","u"};
String newVowel []= {"@","3","!","0","^"};
String newPassword="";
String newValue="";

for(int i=0;i<=oldPassword.length();i++) {
for(int j=0; j<=vowel.length-1;j++) {
newValue=replaceCharacter(oldPassword, vowel[j],newVowel[j]);
}
}

return newPassword;
}

此方法采用给定的字符串并在其中搜索给定的特点。

public static String replaceCharacter
(String password, String toBeReplaced, String replacementCharacter)
{
int move= password.length()-1;
int counter=0;
String string2="";

for(string2 = password.substring(move, password.length()-counter); move>=0; move--) {
if(string2.equals(toBeReplaced)) {
string2=replacementCharacter;
}
else {
string2=password.substring(move+1, password.length()-counter);
}
counter++;
}
return string2;
}

最佳答案

您的解决方案看起来太复杂了。我建议使用这样的东西

public static String enhancePassword(String oldPassword) {
String vowel[] = { "a", "e", "i", "o", "u" };
String newVowel[] = { "@", "3", "!", "0", "^" };
for (int i = 0; i < vowel.length; i++) {
oldPassword = oldPassword.replaceAll(vowel[i], newVowel[i]);
}
return oldPassword;
}

希望对你有帮助!

关于java - 用 Java 数组中的特殊字符替换用户输入中的元音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46250736/

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