java:switch用法和计算年月的方法

import java.util.Scanner;
public class DaYu {
    


    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int y =sc.nextInt();//接收输入的值,不需要重复定义Scanner对象“sc”;
        int m = sc.nextInt();
        int d = sc.nextInt();
        int sum = 0;
        //判断输入是否正确
        if(y<2000||m<0||m>12||d<0||d>31){
            System.out.println("您输入的日期有误,请重新输入");
        }
        //计算输这一年以前的的天数
        for(int k=2000;k<y;k++){
            if(isyear(y)){//if判断语句,如果是Boolean值,不需在后面=true||false;
                sum +=366;
            }else{
                sum +=365;
            }
        }
        //计算输这个月的天数
        for(int n=0;n<m;n++){//累加计算用循环
            sum += ismonth(y,m);
        }
        
        if((sum+d)%5==1||(sum+d)%5==2||(sum+d)%5==3){//求余运算确定具体数字,求余是%不是/
            System.out.println("打渔");
        }else{
            System.out.println("晒网");
        }
        
    }
    //定义年份是否为闰年;如果需要多重计算,且较复杂,应使用构造方法,注意方法返回值的类型
    public static boolean isyear(int y){
        if((y/4==0&&y/100!=0)||(y/400==0&&y/100==0)){
            return true;
        }else{
            return false;
        }
    }
    //定义月份输出的对应天数,涉及几个参数定义几个,统一返回相同的类型;
    public static int ismonth(int y,int m){

            switch(m){
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    return 31;
                case 4:
                case 6:
                case 9:
                case 11:
                    return 30;
                case 2:
                    if(isyear(y)){
                        return 29;                        
                    }else{
                        return 28;
                    }
                default:
                    System.out.println("输入错误");
                    return 0;
            }
    }
}

 

 题目:中国有句老话叫“三天打渔,两天晒网” 。假设有一个人从2000 年1 月1 日开始“三天打渔两天晒网” ,读入三个整数,分别表示年份、月份、日期,问这三个整数所表示的那天这个人在打渔还是在晒网

posted @ 2016-07-03 09:58  小步adc  阅读(1102)  评论(0编辑  收藏  举报