简体   繁体   English

具有递归性的Java中的奇怪行为

[英]Strange behavior in Java with recursivity

I am learning Java. 我正在学习Java。 This is my simple java test. 这是我简单的java测试。

import java.io.Console;
import java.util.regex.Pattern;


public class Pendu {

    private static final int MAX_ERRORS_ALLOWED = 10;
    private static final int MAX_WORDL_LENGHT = 10;

    public static void main(String[] args) {
        String motSaisi = Pendu.readMot();
        System.out.println("Le mot saisi est "+motSaisi);
    }

    public static boolean hasSpecialChar (String s) {
        Pattern p = Pattern.compile("[^a-zA-Z]");
        return (p.matcher(s).find());
    }

    public static String readMot(){
        char[] ChaineSaisi = System.console().readPassword("Joueur 1 : Veuillez saisir un mot sans chiffres ni lettres accentuées: ");
        String text = String.copyValueOf(ChaineSaisi);
        System.out.println("mot a testé "+text);

        if(Pendu.hasSpecialChar(text)) {
            Pendu.readMot();
        }
        System.out.println("mot correct "+text);
        return text;
    }
}

The goal of this is to enter a word witch only characters ( aZ AZ) and only this.When i run this with example like this : 这样做的目的是输入一个单词女巫只有字符(aZ AZ),只有这个。当我用这样的例子运行它:

 Joueur 1 : Veuillez saisir un mot sans chiffres ni lettres accentu?es: 
    mot a test? )))
    Joueur 1 : Veuillez saisir un mot sans chiffres ni lettres accentu?es: 
    mot a test? """
    Joueur 1 : Veuillez saisir un mot sans chiffres ni lettres accentu?es: 
    mot a test? po
    mot correct po
    mot correct """
    mot correct )))
   Le mot saisi est )))

")))" ( This is not the last word i have tpaed, the last is "po"). “)))”(这不是我所说的最后一个词,最后一个是“po”)。 Why this happen ? 为什么会这样?

This is part of the recursion. 这是递归的一部分。 When you first make a recursive call, it will make the current execution process as "pending" so when all the recursion is finished (when there's a special char), it checks the last word typed, then the (n-1)th and so on until it check your very first word. 当你第一次进行递归调用时,它会使当前的执行过程处于“挂起”状态,所以当所有的递归完成时(当有一个特殊的字符时),它会检查输入的最后一个字,然后是第(n-1)个和等等,直到它检查你的第一个字。

Anyway, if you just want to check the validity, this is a terrible way of doing it, since you stack your execution pile at each input. 无论如何,如果你只想检查有效性,这是一种可怕的方法,因为你在每个输入堆叠你的执行堆。 Not only it is ugly, but it can create an OutOfMemory exception 它不仅很难看,而且可以创建一个OutOfMemory异常

Anyway, here's something that should work: 无论如何,这是应该工作的东西:

while (!StringUtils.isAlpha((input = readString())));

StringUtils.isAlpha(myString) returns true is the string is only made of ascii letters. StringUtils.isAlpha(myString)返回true是字符串仅由ascii字母组成。 I suppose that readString() is your read function. 我想readString()是你的读函数。 Here the program will ask you for an input until it is correct. 在这里,程序会询问您输入是否正确。

Your local variables ChaineSaisi and text are local to each call of readMot . 您的本地变量ChaineSaisitext是每次调用readMot本地变量。 That is, every time readMot is called, and these variables are initialised, you're actually getting new instances of these variables. 也就是说,每次调用readMot ,并且这些变量都被初始化时,您实际上正在获取这些变量的新实例。

To do what you want, you'll need to set the text value to whatever is returned by the inner call to readMot . 要执行您想要的操作,您需要将text值设置为内部调用readMot返回的readMot If you change the line inside the if branch to 如果你改变了里面的线if分支

text = Pendu.readMot();

then your program should do what you want, because you will then be setting each text variable to the value from the later call to readMot . 然后你的程序应该做你想要的,因为你将把每个text变量设置为稍后调用readMot的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM