如何减少此代码只使用一个while循环?

时间:2014-10-02 20:01:42

标签: java loops while-loop

到目前为止,这是我的代码:

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;
      }
   }
}

如何将这两个while循环减少到只有一个while循环?

7 个答案:

答案 0 :(得分:2)

将数字存储在两个StringBuilder中,一个用于奇数,另一个用于偶数:

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());
   }
}

答案 1 :(得分:0)

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);
       }

使用两个StringBuilders存储要显示的字符串,然后在完成循环后将其写出。每个循环执行模块化除法(%)以查看其偶数或奇数。如果余数为0则即使它不是,那么它也是奇数。根据它将它附加到适当的StringBuilder。

答案 2 :(得分:0)

减少代码重复通常是件好事。 Java提供的最基本的工具是方法,因此您可以在方法中提取循环的行为并编写等效的程序:

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;
        }
    }
}

请注意,此程序的行为相同:循环将执行两次,但在代码中不会重复。

答案 3 :(得分:0)

使用列表。它们很好地打印出来。

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);
   }
}

答案 4 :(得分:0)

这是一个没有存储数字的解决方案

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++;
    }
}

答案 5 :(得分:0)

如果你决定做一个循环

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;
    }
}

这样做的好处是,您不会构建任何不必要的对象,也不必管理除n之外的任何其他内容。

如果您的目标是减少循环迭代

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);

要注意的事项,this cuts your iterations in half over using a single while with if statements

答案 6 :(得分:0)

只需重新设置计数器即可重新开始迭代:

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;
      }
   }
}