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()'自己調用。你的問題到底是什麼? –