1.设计出一程序,输入四个整数,分别显示出其中的最大值与最小值
import java.util.Scanner;
public class Maxmin {
public static void main(String[] args){
int a=0,b=0,c=0,d=0;
Scanner in=new Scanner(System.in);
a=in.nextInt();
b=in.nextInt();
c=in.nextInt();
d=in.nextInt();
//先两两比较,再将比较结果比较一次
int m,n;
m=a>b?a:b; //a,b中的较大数
n=c>d?c:d; //c,d中的较大数
int x,t;
x=a<b?a:b;
t=c<d?c:d;
int max=0;
int min=0;
max=m>n?m:n;
min=x<t?x:t;
System.out.println("最大数:"+max);
System.out.println("最小数:"+min);
}
}
2.设计出一程序,输入一正整数,代表月份,若是2—4月份,则显示“spring”,若是5—7月份,则显示“summer”,若是8—10月份,显示“fall”,若是11—12月份,显示“winter”
import java.util.Scanner;
public class Jijie {
public static void main(String args[]){
Scanner in=new Scanner(System.in);
int month=in.nextInt();
switch(month){
case 2:
case 3:
case 4:
System.out.println("spring");
break;
case 5:
case 6:
case 7:
System.out.println("Summer");
break;
case 8:
case 9:
case 10:
System.out.println("Fall");
break;
case 11:
case 12:
case 1:
System.out.println("Winter");
break;
}
}
}
迁移:打印12个月分别对应着什么季节
//枚举类型顺次输出12个月对应的季节
import java.util.Scanner;
enum Months{
jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec
}
public class Season {
public static void main(String args[]){
/*System.out.println("请输入一个月份");
Scanner in=new Scanner(System.in);
*/
for(Months month:Months.values()){
switch(month){
case feb:
case mar:
case apr:
System.out.println("Spring");
break;
case may:
case jun:
case jul:
System.out.println("Summer");
break;
case aug:
case sep:
case oct:
System.out.println("Fall");
break;
case nov:
case dec:
case jan:
System.out.println("Winner");
break;
default:
System.out.println("error");
continue;
}
}
}
}