我要打印输出前三个偶数,前三个奇数后,第二个三偶数后,第二个三奇数后
{ 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);
}
}
}
}
最佳答案
解:
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);
}
}
关于java - 如何打印出三个数字(偶数和奇数)?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22245124/