Jave while loops

Asked by At

Beginner at Java. Using Zybooks. To me the software does not teach well, its vague. Here is the Question:

Given positive integer numInsects, write a while loop that prints that number doubled without reaching 200. Follow each number with a space. After the loop, print a newline. Ex: If numInsects = 16, print:

16 32 64 128

Here is what I have:

import java.util.Scanner;

public class InsectGrowth {
   public static void main (String [] args) {
      int numInsects;
      Scanner scnr = new Scanner(System.in);
      numInsects = scnr.nextInt(); // Must be >= 1

       System.out.print(numInsects + " ");

  while (numInsects <= 100) {
     numInsects = numInsects * 2;
     System.out.print(numInsects + " ");
  }

  System.out.println();

   }
}

Here are the results: It did not pass with 200, just left an empty space. Testing with 16. Your output 16 32 64 128 Testing with 98. Your output 98 196 Testing with 200. Output differs. See highlights below. Your output 200 Expected output

5 Answers

0
pcoates On Best Solutions

Move your first print into the while loop and check for 200

public class InsectGrowth {
   public static void main (String [] args) {
      int numInsects;
      Scanner scnr = new Scanner(System.in);
      numInsects = scnr.nextInt(); // Must be >= 1

      while (numInsects < 200) {
          System.out.print(numInsects + " ");
         numInsects = numInsects * 2;
      }

      System.out.println();

   }
}
0
VHS On

This is a problem of handling edge-case scenario. As you see, your while loop is exceeding the boundary condition by one iteration. So you just need to tweak the condition.

Replace your while loop with following code block:

with

     if(numInsects <= 200) {
         System.out.print(numInsects);
     }
     while (numInsects <= 100) {
         numInsects = numInsects * 2;
         System.out.print(" " + numInsects);
     }
0
CmosBattery On
while (true) {
   numInsects = numInsects * 2;
   if (numInsects >= 200) break;
   System.out.print(numInsects + " ");
}

To highlight do it the way that is clearer to you and that there are multiple ways to eat a fish since you're learning, its good to learn you will run into everyone doing it differently.

0
Jesse_mw On

Your while loop is seeing the current value of the numInsects and not considering what's actually being applied.

Example:

import java.util.Scanner;

public class ex {

    public static void main(String[] args) {
        int numInsects;
        Scanner scnr = new Scanner(System.in);
        numInsects = scnr.nextInt(); // Must be >= 1

        System.out.print(numInsects + " ");
        /*
          is  16 less than 200? yes
          is  32  less than 200? yes
          is  64 less than 200? yes
          is  128 less than 200? yes
          is  256 less than 200? no, break

          but if
          is  16 *2  less than 200? yes
          is  32  *2  less than 200? yes
          is  64 *2  less than 200? yes
          is  128  *2 less than 200? no, break
        */

        while (numInsects * 2 < 200) {
            numInsects = numInsects * 2;
            System.out.print(numInsects + " ");
        }

        System.out.println();

    }
}
0
Andre Lança On

your numInsects will be always 0, you should say int numInsects = 1; because your are doing this:

0*2=0 0*2=0 0 0 0 0 0