ihaveacode
ihaveacode

Reputation: 33

2 loops 1 decremented variable

I have an assignment that calls for an input integer, it then decrements that integer all the way down to 1.

it must use a while loop to display that number all the way down to 1 and on another line use a for loop to do the same.

Now after I initiated the while loop, my output for the for loop does not display. Obviously because startNum is now set to 0 after the while loop.

How can I get around this so that I can display the decremented numbers on both lines? Code I currently have:

public class CountDown 
{

    public static void main(String[] args) 
    {
        Scanner input = new Scanner(System.in);

        System.out.print("Enter a number: ");

        int startNum = input.nextInt();

        System.out.println("WHILE LOOP: ");

        while (startNum > 0)
        {
            System.out.print(startNum + " ");
            startNum--;
        }

        System.out.println("\nFOR LOOP:");

        for (int x = 0; x < startNum; x++)
        {
            System.out.print(startNum + " ");

        }

    }

}

Upvotes: 3

Views: 150

Answers (3)

Kick Buttowski
Kick Buttowski

Reputation: 6739

Since Java supports OOP and primitives are passed by value in Java.

why not creating a class and name it ,Num ?

public class Num {

        private int startNum;

        public Num() {

            startNum = 0;
        }

        public int getStartNum() {
            return startNum;
        }

        public void setStartNum(int startNum) {
            this.startNum = startNum;
        }
}

Dirver class become:

public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        Num n = new Num();
        System.out.print("Enter a number: ");

        n.setStartNum(input.nextInt());

        System.out.println("WHILE LOOP: ");
        int counter = n.getStartNum();
        while (counter > 0) {
            System.out.print(counter + " ");
            counter--;
        }

        System.out.println("\nFOR LOOP:");

        for (int x = 0; x < n.getStartNum(); x++) {
            System.out.print(x + " ");

        }

    }

output:

Enter a number: 8
WHILE LOOP: 
8 7 6 5 4 3 2 1 
FOR LOOP:
0 1 2 3 4 5 6 7

Upvotes: 1

Rustam
Rustam

Reputation: 6515

do like this:

public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("Enter a number: ");

        int startNum = input.nextInt();

        System.out.println("WHILE LOOP: ");
        int temp = startNum;           // store startNum in temp variable
        while (startNum > 0) {
            System.out.print(startNum + " ");
            startNum--;
        }

        System.out.println("\nFOR LOOP:");

        for (int x = temp; x >0 ; x--) {   // here iterate through temp.
            System.out.print(x + " ");

        }

    }

Upvotes: 0

nem035
nem035

Reputation: 35481

Have another variable to reset startNum back to its original value, right before the for loop

// ...
int startNum = input.nextInt(); 
int reset = startNum;
// ...
startNum = reset;
for (int x = 0; x < startNum; x++)
// ...

Upvotes: 1

Related Questions