import java.util.Scanner;
public class WordShuffle
{
static Scanner in = new Scanner(System.in);
private static String getText()
{
log("in getText");
String t;
log("Enter only alphabets");
t = in.next();
log("returned t= " + t);
return t;
}
private static void shuffleText(final String txt)
{
log("in shuffle= " + txt);
final int textLenght = txt.length();
final char textArray[] = new char[textLenght];
for (int i = 0; i < textLenght; i++)
{
textArray[i] = txt.charAt(i);
}
for (int i = 0; i < textLenght; i++)
{
}
}
public static void log(final String l)
{
System.out.println(l);
}
private static String validateText()
{
final String text = getText();
log("in validate= " + text + "");
final char[] tc = text.toCharArray();
for (final char t : tc)
{
if (Character.isLetter(t))
{
log("character = " + t);
}
else
{
System.out.println("Error occured, non alphabet found in text");
log("error = " + t);
validateText();
}
}
log("validate returned " + text);
return text;
}
/**
* @param args the command line arguments
*/
public static void main(final String[] args)
{
// TODO code application logic here
shuffleText(validateText());
}
}
,它运行良好, 与smaple文本“ABC3”指出了错误3和复发的validate()方法 的请求再次输入时,如果输入了示例文本“abc”,它将一个接一个地返回文本“abc”和“abc3”。return语句执行两次无环
控制台输出低于
run:
in getText
Enter only alphabets
abc3
returned t= abc3
in validate= abc3
character = a
character = b
character = c
Error occured, non alphabet found in text
error = 3
in getText
Enter only alphabets
abc
returned t= abc
in validate= abc
character = a
character = b
character = c
validate returned abc
validate returned abc3
in shuffle= abc3
BUILD SUCCESSFUL (total time: 8 seconds)
是的,的确如此。因为'validateText()'自己调用。你的问题到底是什么? –