我正在尝试做一个递归方法,它通过一些规则返回一些随机字符。该方法效果很好,但问题是它没有返回我需要的值,也没有在我写的时候停止return myString
这是方法(请看评论):
public String RowGenerator(String sir) {
upper = "";
Log.v(TAG, "RowGenerator Started");
int randI = 1 + (int)(Math.random() * 4);
int randJ = 1 + (int)(Math.random() * 2);
int randK = 1 + (int)(Math.random() * 2);
Log.v(TAG, "inside RowGenerator: randI " + randI + ", randJ " + randJ + ", randK "+ randK);
String upper = sir.replaceAll("[^A-Z]+", "");
sir = sir.replaceAll("[^a-z]+", "");
Log.v(TAG, "String upper " + upper);
//checking if there is a..z & sir>=5
if ((sir.length()>=5) && upper.isEmpty()) {
Log.v(TAG, "sir <5 & !b " + String.valueOf(sir)); // here it prints out exactly what i need, but when i setText id has another value.
sir1.setText(String.valueOf(sir)); // this textview does not get the right value.
return sir; // i want to end the function here and return the value , as i call it as follows: someString = String.valueOf(RowGenerator(sir))
} else if (sir.isEmpty()) {
sir = S[1];
Log.v(TAG, "First sir value: " + sir);
RowGenerator(sir);
} else if (sir.length()<5 && upper.isEmpty()) {
sir = "";
RowGenerator(sir);
}
// if there is A..Z
else if (!upper.equals(null)) {
if(upper.equals(S[0])) {
sir = sir + S[1];
} else if (upper.equals(I[0])) {
sir = sir + I[randI];
} else if (upper.equals(J[0])) {
sir = sir + J[randJ];
} else if (upper.equals(K[0])) {
sir = sir + K[randK];
}
Log.v(TAG, "new value added to sir: " + sir);
RowGenerator(sir);
}
Log.v(TAG, "returning value "); // everything goes great till here, but then this log appears like 5-20 times randomly at each start, it should end in first if, but it got till here
sir1.setText(String.valueOf(sir));
return sir;
}
据我所知,returnmyString();
应该停止方法并返回值,但它没有。请帮我找出我的方法有什么问题。
任何帮助将不胜感激。