如何打印出每三个数字(偶数和奇数)?

时间:2014-03-07 08:32:26

标签: java

我要打印输出前三个偶数,前三个奇数后,后三个偶数后二个三个奇数

{ Like 
Even Number 
0 
2 
4  
Odd Number 
1 
3 
5
Even Number 
6 
8 
10
Odd Number 
7 
9 
11 }

代码:

public class OddAndEven {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    int i;
    for(i=0;i<=50;i++){
        if(i%2==1){
            System.out.print("Odd Number "+i);
        }
        if(i%2==0){
            System.out.print("     ");
            System.out.println("Even Number "+i);
        }
    }

}

}

6 个答案:

答案 0 :(得分:5)

一种简单的方法

    for(i=0;i<=50;i = i + 6){
        System.out.println("Even Number");
        logIt (i);
        logIt (i + 2);
        logIt (i + 4);

        System.out.println("Odd Number");
        logIt (i + 1);
        logIt (i + 3);
        logIt (i + 5);

    }

private void logIt (int i) {
   if (i <= 50) System.out.println(i);
}

答案 1 :(得分:0)

试试这个: -

public static void main(String str[]){



        final int step = 3;

        for (int s = 0; s < 50; s += step * 2) {

            System.out.println("Even number");
            for (int i = 0; i < step; i++) {
                int val=(s + i * 2);
                if(val<=50)
                System.out.println(val);
            }


            System.out.println("Odd number");
            for (int i = 0; i < step; i++) {
                int val=(s + i * 2 + 1);
                if(val<=50)
                System.out.println(val);
            }
        }
 }

输出

Even number
0
2
4
Odd number
1
3
5
Even number
6
8
10
Odd number
7
9
11
Even number
12
14
16
Odd number
13
15
17
Even number
18
20
22
Odd number
19
21
23
Even number
24
26
28
Odd number
25
27
29
Even number
30
32
34
Odd number
31
33
35
Even number
36
38
40
Odd number
37
39
41
Even number
42
44
46
Odd number
43
45
47
Even number
48
50
Odd number
49

希望它会对你有所帮助。

答案 2 :(得分:0)

希望你喜欢这个干净的版本:

public static void main(String[] args) throws Exception {
    for (int value = 4; value <= 50; value += 4) {
        print(value++, "Even");
        print(value++, "Odd");
    }
}

private static void print(int value, String prefix) {
    System.out.println(prefix + " number");
    for (int diff = 4; diff >= 0; diff -= 2) System.out.println(value - diff);
}

答案 3 :(得分:0)

生产者的另一个解决方案:

public static void main (String[] args) {

    OddEvenSequenceProducer producer = new OddEvenSequenceProducer();
    for (int i = 0; i < 50; i++) {
        System.out.println(producer.next());
    }
}

private static class OddEvenSequenceProducer {

    private final int step = 3;
    private int run;
    private int i;
    private boolean odd;

    public int next () {

        int value = (run * step + i++) * 2 + (odd ? 1 : 0);
        if (i == step) {
            i = 0;
            if (odd)
                run++;
            odd = !odd;
        }
        return value;
    }
}

答案 4 :(得分:0)

public class TrazenjeBrojeva 
{
public static void main(String args[])
{
    int j=0, k=1;
    for(;k<50;)
    {
        for(int firstLoop=0;firstLoop<3;firstLoop++)
        {
            if((j%2)==0)
            {
                if(j>50)
                {
                    break;
                }
                System.out.println("Number is even: "+j);
                j+=2;

            }
        }

        for(int secondLoop=0;secondLoop<3;secondLoop++)
        {
            if(!((k%2)==0))
            {
                if(k>50)
                {
                    break;
                }
                System.out.println("Number is odd: "+k);
                k+=2;

            }
        }

    }
}
}

只有if和for循环才能正常工作

答案 5 :(得分:-1)

解决方案:

final int step = 3;

for (int s = 0; s < 50; s += step * 2) {

    System.out.println("Even number");
    for (int i = 0; i < step; i++) {
        System.out.println(s + i * 2);
    }
    System.out.println("Odd number");
    for (int i = 0; i < step; i++) {
        System.out.println(s + i * 2 + 1);
    }
}