2014-03-07 214 views
0

我想打印輸出第一三連號,後前三奇數,之後第二個三連號之後第二個三奇數如何打印出每個三個數字(偶數和奇數)?

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

} 

}

+0

,問題是?? – laaposto

+0

它是作業還是什麼?我喜歡'// TODO自動生成的方法存根'行 –

+0

您可以嘗試@Peter Walser答案。其工作.. –

回答

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

根據我的舊回答過去了50年 –

+0

關於原始問題的限制沒有任何說法,但在打印它們之前不應該很難檢查數字。 –

+1

是的,它確實是一個很好的邏輯。謝謝。 –

5

簡單way

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

根據問題從「0」開始。給出想法比回答這樣的問題要好。 –

+1

它最多隻能打印54份,你能否儘快糾正它。 – newuser

+0

非常好,簡單的答案。 +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 

希望它會幫助你。

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

      } 
     } 

    } 
} 
} 

只有當和for循環,它的工作原理