[英]Replacing vowels from user input with special characters from an array in Java
您好Stack Overflow用户,我正在尝试用不同的字符替换用户输入中的元音。 下面的第一个方法通过用指定的特殊字符替换每个元音来增强作为String参数传入的密码。 我唯一的问题是在主机上打印时,我需要输入的密码才能更换元音。 例如,如果输入“ 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;
}
此方法采用给定的String并在其中搜索给定的字符。
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;
}
希望能帮助到你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.