vendredi 4 décembre 2015

recursive method to display a string backwards but while loop doesn't work

everyone. I tried to write a recursive method that displays the contents of a string backwards. And I don't understand why the while loop in the method doesn't work as I expected, it keeps looping and looping. However, if I use if/else loop, it seems to work perfectly. But I just want to understand what happens to the while loop. Thank you very much for your help in advance. :)

 public class printString{

    void BitString(String a, int i){

        while (i > 0){
            System.out.println(i);
            //System.out.println(a.substring(i-1, i));
            i--;
            BitString(a.substring(0, i), i);

        }
        return;

    }

    public static void main(String args[]){
        printString print = new printString();

        String str = "I am a beginner.";
        int i = str.length() -1;

        print.BitString(str, i);

    }

}

Aucun commentaire:

Enregistrer un commentaire