Search code examples
javacounter

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

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