简体   繁体   中英

Why does my loop not end on meeting a specific condition?

Problem:

Whenever I use a while loop to ask the user for input n times, it goes past that condition, ignores it, and then loops an additional time.

For example, if I want my while loop to execute 5 times, it will instead execute 6 times.

What it does currently: it starts with asking: Enter Number 0: until it reaches Enter Number 4. However, instead of ending at number 4, it continues to ask for Number 5. So in total, there are 6 Numbers, from Number 0 to Number 5.

I should also add: you can type @@ to stop entering numbers, then it will exit that loop and do something with those numbers.

Launcher.java

/* Global Variables */
static final int MAXTIMESTOLOOP = 5;
static LinkedList<String> numberList = new LinkedList<String>();
static int totalNumbers = 0;

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

       System.out.print("Enter number " + totalNumbers + ". Type @@ to stop.");
       numberInput = input.next();

       while(!numberInput.equals("@@") && totalNumbers < MAXTIMESTOLOOP) {
             /* Add number to numberList */
             numberList.add(numberInput);
             totalNumbers++;
             System.out.print("Enter number " + totalNumbers + ". Type @@ to stop.");
             numberInput = input.next();

       }
}

Unintended Output

Enter number 0. Type @@ to stop. 5
Enter number 1. Type @@ to stop. 8
[..]
Enter number 4. Type @@ to stop. 12
Enter number 5. Type @@ to stop. 14

Instead, I want it to stop at number 4, since that is when it should terminate.

Intended Output

Enter number 0. Type @@ to stop. 12
Enter number 1. Type @@ to stop. 4
[...]
Enter number 4. Type @@ to stop. 1

Rather than continuing to number 5, I want it to stop at number 4. But I cannot seem to pinpoint what is the cause of the reason.

Suspected Reasons

After debugging the code, I found that the variable totalNumbers increases to 5 and then loops back around. I don't understand why that is since I have the condition: totalNumbers < MAXTIMESTOLOOP that is the same as while(0 < 5)

The first number is entered before your while loop, but does not increment TOTALNUMBERS. Within your loop, the continuation condition of TOTALNUMBERS < MAXTIMESTOLOOP is met when TOTALNUMBERS=4, but is incremented within the loop, displaying the 5th entry prompt.

I would modify the loop to move the test condition to the end, and eliminate the extraneous input.

   do {
         /* Add number to numberList */
          System.out.print("Enter number " + TOTALNUMBERS + ". Type @@ to stop.");
         numberInput = input.next();
         if (!numberInput.equals("@@")){
             numberList.add(numberInput);
             TOTALNUMBERS++;
         }
   }
   while (!numberInput.equals("@@") && TOTALNUMBERS < MAXTIMESTOLOOP);

You could try to use the do-while loop, as follows:

static final int MAXTIMESTOLOOP = 5;
static LinkedList<String> numberList = new LinkedList<String>();
static int TOTALNUMBERS = 0;

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

    do{
        System.out.print("Enter number " + TOTALNUMBERS + ". Type @@ to stop.");
        numberInput = input.next();

        /* Add number to numberList */
        numberList.add(numberInput);
        TOTALNUMBERS++;
    }
    while (!numberInput.equals("@@") && TOTALNUMBERS < MAXTIMESTOLOOP);
}

The two lines before the while loop are redundant since you're executing them anyway inside the loop.

Your main method should look like this:

public static void main (String [] args){ 

    Scanner input = new Scanner(System.in);
    String numberInput = "";

    while(!numberInput.equals("@@") && totalNumbers < MAXTIMESTOLOOP) {

             System.out.print("Enter number " + totalNumbers + ". Type @@ to stop.");
             numberInput = input.next(); 
             /* Add number to numberList */
             if(!numberInput.equals("@@")){
             numberList.add(numberInput);}
             totalNumbers++;

       }
       }

Notice I moved some things around in the loop. I moved TOTALNUMBERS++ to the end because you want the first print statement to print 0 not 1.

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