我试图做两件事:
现在是我的代码:
public class Phonias{
static String[] consonnant={"B","C","D","F","G","H","J","K","L","P","Q","R","S","T","V","W","X","Z"};
static String [] vowels={"A","E","I","O","U","Y"};
public static String phonias1(final String str){
if (str == null){
return "null";
}
if(str==""){
return "";
}
if (str.contains("S")){
}
for (int i = 0; i < consonnant.length; i++) {
if(str.endsWith(consonnant[i]+"S") && str.length()>3){
String temp = str.replaceAll(consonnant[i]+"S",consonnant[i]+"");
return temp;
}
}
for (int i = 0; i < vowels.length; i++) {
if( str.contains(vowels[i]+"S"+vowels[i])){
return str.replace("S"+vowels[i], "Z"+vowels[i]);
}
}
return str;
}
}
我的所有测试都运行良好,除了&#34; ISISIDORETS&#34;它归还给我&#34; ISISIDORET&#34;。
答案 0 :(得分:2)
您在第一个循环中返回temp
,因此永远不会执行第二个循环。只需更新tampon
并最后将其返回。像这样:
for (int i = 0; i < consonnant.length; i++) {
if(tampon.endsWith(consonnant[i]+"S") && tampon.length()>3){
tampon = tampon.replaceAll(consonnant[i]+"S",consonnant[i]+"");//supprime le "S" final s'il est situé après une consonne
}
}
for (int i = 0; i < vowels.length; i++) {
if( tampon.contains(vowels[i]+"S"+vowels[i])){
tampon = tampon.replace("S"+vowels[i], "Z"+vowels[i]);//si le "S" précède une voyelle, remplacer par "Z"
}
}
return tampon;