Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to fix count down problem Java using counter

I’m trying to create a code that uses a counter to countdown from int n.
I’m able to count up just fine, but when I execute the code, it does not countdown at all.
Does anybody know whereabouts I went wrong?

public void countUp(int n) {
        System.out.println("*** Counting up " + n);
        for (int step = 1; step <= n; step++) {
            System.out.println("counter = " + currentCount);
            currentCount += 1;
        }
    }
    public void countDown(int n){
        System.out.println("*** Counting down " + n);
        for (int step = 1; step >= n; step--){
            System.out.println("counter = " +currentCount);
            currentCount -= 1;
        }
    }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

public void countDown(int n){
    for (int step = n; step >= 0; step--){
        System.out.println("counter = " + step );
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading