如何将这两个while语句合并为一个?

时间:2016-09-23 13:05:01

标签: java while-loop

我有两个while循环,其中一个是50到100之间的奇数,另一个是从50到100之间取相同的偶数。它打印得很好并且它有效但我的教授要我转换它分为一个while循环而不是两个。我遇到了麻烦,因为我之前按顺序使用了print语句,所以当数字进入时它就适合了。

 int e = 50;
 int o = 51;
 System.out.print("Even numbers between 50 and 100: 50,");
 while (e <= 98){
     e += 2;
     if (e%2 == 0){
        System.out.print(e + ",");
     }
 }
 System.out.print("\nOdd numbers between 50 and 100: 51,");
 while (o <= 97){
     o+= 2;

     if (e%1 == 0) { 
         System.out.print(o + ",");
     }
}


Even numbers between 50 and 100: 50,52,54,56,58,60,62,64,66,68,70,72,74,76,78,80,82,84,86,88,90,92,94,96,98,100,
Odd numbers between 50 and 100: 51,53,55,57,59,61,63,65,67,69,71,73,75,77,79,81,83,85,87,89,91,93,95,97,99,

输出现在看起来像这样,但我需要能够用一个循环代替

6 个答案:

答案 0 :(得分:3)

如果你只需要一个while循环和单独的行,你应该构建你的字符串并在之后打印它们。 类似的东西:

StringBuilder odds = new StringBuilder();
StringBuilder evens = new StringBuilder();
i = 50;
while(i<100) {
    if(i%2 == 1) {
        odds.append(i).append(",");
    }
    else {
        evens.append(i).append(",");
    }
    i++;
}
// remember that odds and evens now probably ends with a ','which you should remove
odds.removeCharAt(odds.size());
evens.removeCharAt(evens.size());
// or do something like the following:
//odds.append(" and nothing more!");
//evens.append(" and nothing more!");

System.out.println("Odd numbers are: ");
System.out.println(odds);
System.out.println("Even numbers are" ");
System.out.println(evens)

答案 1 :(得分:1)

尝试在每个步骤中将计数器变量递增1并使用if语句检查模数(%)。如果它等于0,那么数字是偶数;如果它等于1,那么数字是奇数。要单独打印,可以使用两个列表或数组。

int count=50;
StringBuilder odds = new StringBuilder();
StringBuilder evens = new StringBuilder();

while(count<=100)
{
if(count%2==0) // here, the number is even, append this number to even string
   evens.append(count).append(",");

else // here, the number is odd, append this number to odds string
   odds.append(count).append(",");
count++;
}

// when everything is done, print the strings
// before doing that, you should remove the last comma at the end of these Strings. 
// Otherwise, you will have something like this: 93,95,97,

evens.setLength(evens.length() - 1); // to remove the comma at the end
odds.setLength(odds.length() - 1); // to remove the comma at the end

System.out.println("The even numbers are: " + evens.toString());
System.out.println("The odd numbers are: " + odds.toString());

使用StringBuilder而不是使用+=附加String s更高效。看看这个链接:

https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html

答案 2 :(得分:0)

您可以像这样使用单个while循环来打印奇数和偶数。

int e = 50;
    while (e <= 98)
    {       
        if (e%2 == 0)
        {
            System.out.println("even :"+e);
        }
        else
        {
            System.out.println("odd :"+e);
        }
        e++;
    }

答案 3 :(得分:0)

只需创建2个字符串输出变量,并根据条件

将它们填入循环中
if (i%2==1)
  odd_output = odd_output + "," + i.toString 
else
  even_output = even_output + "," + i.toString 

(我在Java语法中可能错了)

然后用你喜欢的任何其他信息打印两个结果字符串

答案 4 :(得分:0)

此处您不需要维护任何单独的ArraysStrings,但条件复杂

public class EvenOddTest {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int num = 50;
        System.out.println("Even numbers between 50 and 100: ");
        while (num < 100){
            if (num%2 == 0){
                System.out.print(num + ",");
            } else {
                System.out.print(num + ",");
            }
            if( num == 98 ) {
                num = 49;
                System.out.println();
                System.out.println("Odd numbers between 50 and 100: ");
            }
            num += 2;
        }
    }
}

答案 5 :(得分:0)

我知道它没有直接回答问题(尽管输出是相同的)但是在2016年,当我们想要打印一系列数字时,我们会这样做:

public static void main(String[] args) {
    int[] evenNumbers = IntStream.rangeClosed(50, 100).filter(i -> i % 2 == 0).toArray();
    int[] oddNumbers = IntStream.rangeClosed(50, 100).filter(i -> i % 2 != 0).toArray();

    System.out.println("Even numbers between 50 and 100: " + Arrays.toString(evenNumbers));
    System.out.println("Odd numbers between 50 and 100: " + Arrays.toString(oddNumbers));
}

如果你的目标是学习如何使用while循环,那么我的解决方案就没用了。但是,如果你的目标是像现在一样练习Java,我认为如果达到了目标。