简体   繁体   中英

Strange behaviour when using recursion in an if statement, inside a method. Java

While learning Java by doing online courses, I'm experimenting with some code written with the tutorial. There is a method, that should concatenate "courses" and in the end just print them out. I wanted to use recursion instead of loop.

private String courses = "";
private int tuitionBalance = 0;

public void enroll() {
    System.out.println("Enter course to enroll or \"Q\" to quit.");
    Scanner in = new Scanner(System.in);
    String course = in.nextLine();
    if (!course.equalsIgnoreCase("q")) {
        courses = courses + "\n" + course;
        tuitionBalance = tuitionBalance + costOfCourse;
        enroll();
    }

    System.out.println("Enrolled in: " + courses);
    System.out.println("Tuition balance: " + tuitionBalance);
    return; // even with "return", this method executes multiple times after leaving if statement.
}

When this method runs, it keeps prompting for input untill 'Q' is pressed. Concatenation works fine. Then it leaves "if statement" and (what is strange for me) it runs last two lines of println code as many times, as the input was provided.

For example when I enter three types of "course" and then press 'Q', the output is:

Enter course to enroll or "Q" to quit.
History  // My entry
Enter course to enroll or "Q" to quit.
Math  // My entry
Enter course to enroll or "Q" to quit.
Biology  // My entry
Enter course to enroll or "Q" to quit.
q  // My entry
Enrolled in: 
History
Math
Biology
Tuition balance: 1800
Enrolled in: 
History
Math
Biology
Tuition balance: 1800
Enrolled in: 
History
Math
Biology
Tuition balance: 1800
Enrolled in: 
History
Math
Biology
Tuition balance: 1800

Process finished with exit code 0

Could You please tell me what is happening?

EDIT: I've read Understanding recursion [closed] and I haven't found any information about how recursion affects the call stack. There is much excellent information on how to use recursion, but still I haven't found the answer to my question there.

The thing is you don't want recursions to print for cases where users have entered a course a simple else will solve the problem

private String courses = "";
private int tuitionBalance = 0;

public void enroll() {
    System.out.println("Enter course to enroll or \"Q\" to quit.");
    Scanner in = new Scanner(System.in);
    String course = in.nextLine();
    if (!course.equalsIgnoreCase("q")) {
        courses = courses + "\n" + course;
        tuitionBalance = tuitionBalance + costOfCourse;
        enroll(); 
    }
    else{//now it would come here only once after user has entered q after which it returns
    System.out.println("Enrolled in: " + courses);
    System.out.println("Tuition balance: " + tuitionBalance);
     }
    return;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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