return语句执行两次没有循环

时间:2013-12-15 18:49:42

标签: java

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());
    }
}

如果使用示例文本“abc”运行,它运行正常, 使用smaple文本“abc3”它指出错误3并重复validate()方法 再次请求输入时,如果输入样本文本“abc”,则一个接一个地返回文本“abc”和“abc3”。

控制台输出低于

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)

5 个答案:

答案 0 :(得分:2)

问题出在您的else块中,您以递归方式调用validateText(),并忽略返回值。在这种情况下,该方法将以任何方式返回用户输入的第一个值。您应该将else块更改为:

else { 
        System.out.println("Error occured, non alphabet found in text");
        log("error = " + t);
        return validateText();  // Return the result
    }

答案 1 :(得分:2)

这是因为你在失败时没有打破执行。该错误在validateText递归中,代码应为:

return validateText();

答案 2 :(得分:2)

我按照建议做了,这是新的validateText()方法

private static String validateText() {
    String text = getText();
    log("in validate= " + text + "");
    char[] tc = text.toCharArray();

    for (char t : tc) {
        if (Character.isLetter(t)) {
            log("character = " + t);
        }
        else {
            System.out.println("Error occured, non alphabet found in text");
            log("error = " + t);
            log("validate returned " + text);
            return validateText();
        }
    }
    return text;
}

我在else中返回了validateText(),必须返回最终的正确文本。

答案 3 :(得分:0)

vaidateText()以递归方式调用。因此,在第二次“正确”输入之后,第一次方法调用结束后返回第一个“不正确”的输入。

答案 4 :(得分:0)

以上答案是正确的。同时递归调用函数可能会导致溢出,所以也许你应该重新考虑你的设计。为什么不使用简单的正则表达式呢?谷歌:正则表达式,正则表达式java 这是每个人都使用的。