我想將第一個方法更改爲while和for循環。我已經添加了下面的代碼。它是否正確?Java - 將方法更改爲for/while循環
public String extractWordFour(String text){
if(text.length()==0 || text.charAt(0) == ''){
return "";
} else {
return text.charAt(0) + extractWordFour(text.substring(1));
}
}
public String extractWordFour(String text){
int i=0;
while (i<=text.length){
return text.charAt(0) + extractWordFour(text.substring(i));
i++;
}
}
public String extractWordFour(String text){
for(int i=0;i<=text.length();i++){
return text.charAt(0) + text.substring(1);
}
}
第一個代碼不能編譯。 – assylias
還是我需要添加'||在while循環測試中的text.charAt(0)=='''? – nsc010
至於我,它看起來很奇怪。該方法的目的是什麼? – jdevelop