简体   繁体   中英

How can I reduce this code to use only one while loop?

Here's my code so far:

public class EvenOdd
{
   public static void main(String[]args)
   {
      System.out.print("Even numbers between 50 and 100: ");
      int e = 50;
      while (e <= 100) {
         System.out.print(" " + e);
         e += 2;
      }

      System.out.print("\nOdd numbers between 50 and 100: ");
      int i = 51;
      while (i <= 100) {
         System.out.print(" " + i);
         i += 2;
      }
   }
}

How can I reduce these two while loops to just one while loop?

Store the numbers in two StringBuilder s, one for the odd numbers and the other for the even numbers :

public class EvenOdd
{
   public static void main(String[]args)
   {
      StringBuilder even = new StringBuilder();
      StringBuilder odd = new StringBuilder();

      int e = 50;
      while (e <= 100) {
         if (e%2 == 0)
             even.append (" " + e);
         else
             odd.append (" " + e);
         e++;
      }
      System.out.print("Even numbers between 50 and 100: " + even.toString());
      System.out.print("\nOdd numbers between 50 and 100: " + odd.toString());
   }
}
public static void main(String[]args)
       {
          StringBuilder evenString = new StringBuilder("Even numbers between 50 and 100: ");
          StringBuilder oddString = new StringBuilder("Odd numbers between 50 and 100: ");
          int e = 50;
          while (e <= 100) {
             if((e % 2) == 0)
             {
                 evenString.append(" " + e);
             }
             else
             {
                 oddString.append(" " + e);
             }
             e++;
          }
          System.out.println(evenString);
          System.out.println(oddString);
       }

Use a two StringBuilders to store the strings you want to display then write them out after your done looping. Each loop do modular division (%) to see if its even or odd. If the remainder is 0 then it's even if it's not then it's odd. Based on which one it is just append it to the appropriate StringBuilder.

Reducing code duplication is often a good thing. The most basic tool that Java provides are methods, so you could extract the behavior of your loop in a method and write an equivalent program:

public class EvenOdd
{
    public static void main(String[]args)
    {
        System.out.print("Even numbers between 50 and 100: ");
        printEverySecondNumber(50, 100);

        System.out.print("\nOdd numbers between 50 and 100: ");
        printEverySecondNumber(51, 100);
    }

    public static void printEverySecondNumber(int start, int end){
        int current = start;
        while (current <= end) {
            System.out.print(" " + current);
            current += 2;
        }
    }
}

Note that this program behaves just the same: The loop will stil get executed twice, but it is not duplicated in code.

Use lists. They print out nicely.

public class EvenOdd
{
   public static void main(String[]args)
   {
     List<String> even = new ArrayList<String>();
      List<String> odd = new ArrayList<String>();

      int e = 50;
      while (e <= 100) {
         if (e%2 == 0)
             even.add(String.valueOf(e));
         else
             odd.add(String.valueOf(e));
         e++;
      }
      System.out.print("Even numbers between 50 and 100: " + even);
      System.out.print("\nOdd numbers between 50 and 100: " + odd);
   }
}

Here is a solution without storing numbers

public static void main(String[] args) {
    int i = 0; 
    System.out.print("Even numbers between 50 and 100: ");
    while (i <= 50) {
        if (i == 26) { 
            System.out.print("\nOdd numbers between 50 and 100: ");
        }
        if (i <= 25) {
            System.out.print (" " + (2 * i + 50));
        } else {
            System.out.print (" " + (2 * (i - 25) + 49));
        }
        i++;
    }
}

If you are determined to do a single loop

int n = 50;
System.out.print("Even numbers between 50 and 100:");
while(n < 151){
    if(n <= 100)
        System.out.print(" " + n);
    else
        System.out.print(" " + (n-50));

    if(n != 100)
        n = n + 2;
    else{
        System.out.print("\nOdd numbers between 50 and 100:");
        n = n + 1;
    }
}

The benefit to this is you aren't building any unnecessary objects or having to manage anything other than n really.

If your goal is to reduce loop iterations

StringBuilder evenString = new StringBuilder();
StringBuilder oddString = new StringBuilder();

int n = 50;
while(n <= 100){
    evenString.append(" " + n);
    if(n != 100)
        oddString = oddString.append(" " + (n + 1));
    n = n + 2;
}

System.out.println("Even numbers between 50 and 100:" + evenString);
System.out.print("Odd numbers between 50 and 100:" + oddString);

Something to notice, this cuts your iterations in half over using a single while with if statements .

Just re-start iterations by re-setting the counter:

public class EvenOdd
{
   public static void main(String[]args)
   {
      int e = 0;
      while (1) {
         if (e==0) {
             System.out.print("Even numbers between 50 and 100:");
             e = 50;
         }
         else
         if (e==102) { 
             System.out.print("\nOdd numbers between 50 and 100:");
             e = 51;
         }
         else
         if (e==101) { 
             System.out.print("\n");
             break;
         }

         System.out.print(" " + e);
         e += 2;
      }
   }
}

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