Jerry Murphy
Jerry Murphy

Reputation: 348

why does my loop only run once?

This is my code

executeProgram:
    for (int i= 0; 1< memory.length; i++)
    {   
        String twoDigitMemory = String.format("%02d",memory[i] );
        System.out.print(twoDigitMemory +" ? +");
        accumulator = input.nextInt();

        if (input.nextInt() == -99999)
        {
            System.out.println("*** Program loading completed ***");
            System.out.println("*** Program execution begins  ***");
            break executeProgram;
        }
    }

This is my output. I enter 123.

***            Welcome to Simpletron!            ***
*** Please enter your program one instruction    ***
*** (or data word) at a time. I will display     ***
*** the location number and a question mark (?)  ***
*** You then type the word for that location.    ***
*** Type -99999 to stop entering your program    ***
00 ? +123

after entering 123 and pressing enter I expect the for loop to run again printing "00 ? +" but nothing happens, the input does get saved to the variable

Upvotes: 1

Views: 122

Answers (1)

SMA
SMA

Reputation: 37023

You are expecting user to input number twice. Once you are storing in accumulater while the other just to compare the number like if (input.nextInt() == -99999) which is not needed. Instead you should use accumulater variable for checking like:

if (accumulater == -99999)

Upvotes: 2

Related Questions