入门基础练习
文章目录练习一练习二练习三练习四
练习一package ifDemo;
public class Test1 {
public static void main(String[]args){
int i=16;
if(i%2==0){
System.out.println("是偶数");
}else{
System.out.println("是奇数");
}
}
}
练习二
package ifDemo;
public class Test2 {
public static void main(String[] args) {
int i=15;
if(i%3==0 && i%5==0){ //或者i%15==0
System.out.println("乒乓");
}else if(i%5==0){
System.out.println("乓");
}else if(i%3==0){
System.out.println("乒");
}else{
System.out.println(i);
}
}
}
练习三
package ifDemo;
public class Test3 {
public static void main(String[] args) {
int month=8;
if(month==3 || month==4 || month==5){ //或者 3<=month && month<=5
System.out.println("春季:春眠不觉晓");
}else if(month==6 || month==7 || month==8){ //或者 6<=month && month<=8
System.out.println("夏季:夏天蚊子咬");
}else if(month==9 || month==10 ||month==11){ //或者 9<=month && month<=11
System.out.println("秋季:秋天一身包");
}else if(month==12 || month==1 || month==2){
System.out.println("冬季:蚊子死翘翘");
}else{
System.out.println("月份有误");
}
}
}
练习四
package ifDemo;
public class Test4 {
public static void main(String[] args) {
int year=2020;
int month =2;
if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12){
System.out.println("31天");
}else if(month==4 || month==6 || month==9 || month==11){
System.out.println("30天");
}else if(month==2 && year%4==0){
System.out.println("29天");
}else if(month==2 && year%4!=0){
System.out.println("28天");
}else{
System.out.println("月份有误");
}
}
}



