这是来自过去试卷的问题。我不太确定如何将extract
方法转换为while和for循环。Java - 将此方法转换为for和while循环
我试过这个问题:extract1
和extract2
方法,但我知道他们是不正确的。原始方法可能没有用处,但考试要求您展示如何以不同方式编写方法。我想知道他们如何做以备将来参考。
String extractedThis = "";
public String extract(String text){
if(text.length()==0){
return extractedThis;
} else {
return extractedThis = text.charAt(0) + extract(text.substring(1));
}
}
public String extract1(String text) {
while (text != null) {
extractedThis = text.charAt(0) + text.substring(1);
}
return extractedThis;
}
public String extract2(String text) {
for (int i = 0; i < text.length(); i++) {
extractedThis = text.substring(i);
}
return extractedThis;
}
你测试你的方法是什么? – codingbiz