使用java编写,分别打印出1到100间的偶数和奇数

2024-11-22 09:57:54
推荐回答(5个)
回答1:

public class Main{
public static void main(String args[]){
System.out.println("打印偶数:");
for( int i = 1; i <=100; i++){
if(i%2==0){
System.out.print(i+" ");
}
}
System.out.println();
System.out.println("打印奇数:");
for(int i = 1; i<=100; i++ ){
if(i%2==1){
System.out.print(i+" ");
}
}
}
}

利用求摸运算符就可以

回答2:

public class MyTest{

public static void main(String[] args){

for(int i=0;i<100;i++){
if(i%2==0)
System.out.println(i+"是偶数");
else
System.out.println(i+"是奇数");
}

}

回答3:

public class jiajia {

public static void main(String[] args) {
int i;
for(i=1;i<=100;i++)
{
if(i%2==0){
System.out.println(i+"是偶数");
}else{
System.out.println(i+"是奇数");
}
}

}

}

回答4:

public
class
Main{
public
static
void
main(String
args[]){
System.out.println("打印偶数:");
for(
int
i
=
1;
i
<=100;
i++){
if(i%2==0){
System.out.print(i+"
");
}
}
System.out.println();
System.out.println("打印奇数:");
for(int
i
=
1;
i<=100;
i++
){
if(i%2==1){
System.out.print(i+"
");
}
}
}
}
利用求摸运算符就可以

回答5:

public static void main(String[] args){
for(int i=0;i<100;i++,System.out.println(i+(i%2==0?":偶数":":奇数")));
}

//一行搞定