循环语句练习

`

1、计算这一天是这一年的第几天
案例需求:
(1)从键盘分别输入年、月、日,判断这一天是当年的第几天。
(2)要求要对输入值进行合法性的判断,确保输入的年份值必须大于0,月份值必须在[1,12]之间,日期值必须在[1, 当月最大日期值]范围内。

package HomeWork;

import java.util.Scanner;

/**
 * ClassName: HomeWork05
 * Package: HomeWork
 * Description:
 * @Author FYY
 * @Create 2024/5/30 
 * @Version 1.0
 */
public class HomeWork11 {
    public static void main(String[] args) {
        //创建扫描器对象
        Scanner sc = new Scanner(System.in);

        //todo 对键盘录入的时间日期进行校验
        int year; //年
        while (true){
            //键盘录入年份
            System.out.println("请输入一个年份:");
            year = sc.nextInt();
            if (year>0)break;else System.out.println("年份必是须大于0的整数");
        }
        int monch;  //月
        while (true){
            //键盘录入月份
            System.out.println("请输入"+year+"年的月份:");
            monch = sc.nextInt();
            if (monch>0 && monch<13)break;else System.out.println(year+"年的月份必须在[1,12]范围之内!不能超出五行!!!");
        }
        int sun;    //日
        int yue = 0;    //定义当月天数
        while (true){
            if (monch==1 || monch==3 || monch==5 || monch==7 || monch==8 || monch==10 || monch==12) yue=31;
            else if (monch==4 || monch==6 || monch==9 || monch==11) yue=30;
            else if (monch==2){
                yue = year%400==0 || year%4==0&&year%100!=0?29:28;
            }
            //键盘录入日期
            System.out.println(year+"年"+monch+"月多少号?");
            sun = sc.nextInt();
            if (sun>0 && sun<=yue)break;else System.out.println("您输入天数有误!"+year+"年"+monch+"月一共才有:"+yue+"天");
        }
        //关闭资源(及时止损)
        sc.close();

        //todo 校验完成,计算改时间是该年的第几天
        //定义总共的天数
        int sum = sun;
        for (int i=1;i<monch;i++){
            if (i==4 || i==6 || i==9 || i==11) sum += 30;
            else if (i==2) sum += year%400==0 || year%4==0&&year%100!=0?29:28;
            else sum +=31;
        }
        //打印结果
        System.out.println(year+ "年" +monch+ "月" +sun+"日,是"+year+"的第:"+sum+"天!");
    }
}

  1. 计算这一天是在打鱼还是晒网
    (1)假设从2000年1月1日开始三天打鱼,两天晒网,
    (2)从键盘输入今天的日期年、月、日,显示今天是打鱼还是晒网
package HomeWork;

import java.util.Scanner;

/**
 * ClassName: HomeWork12
 * Package: HomeWork
 * Description:
 * @Author FYY
 * @Create 2024/5/30
 * @Version 1.0
 */
public class HomeWork12 {
    public static void main(String[] args) {
        //todo 校验键盘录入的年月日
        //todo 假设从2000年1月1日开始三天打鱼,两天晒网(年份必须大于等于2000)
        //创建扫描器对象Scanner,并关联键盘System.in
        Scanner sc = new Scanner(System.in);
        //年
        int year;
        while (true){
            //录入年份
            System.out.println("请您输入一个年份:");
            year = sc.nextInt();
            if (year>=2000)break;else System.out.println("您输入的年份应该在2000年之后,请重新输入:");
        }
        //月
        int month;
        while (true){
            //录入月份
            System.out.println("请您输入"+year+"年的月份!");
            month = sc.nextInt();
            if (month>0&&month<13)break;System.out.println("您输入"+year+"年的月份有误,应在[1~12]之间,请重新输入:");
        }
        //日
        int sun;
        while (true){
            //录入日
            System.out.println("请您输入"+year+"年"+month+"月的多少号?");
            sun = sc.nextInt();
            //判断该年该月的总天数
           /* 平年的2月份有28天,闰年的2月份有29天。
            1月、3月、5月、7月、8月、10月、12月有31天,
            4月、6月、9月、11月有30天。*/
            //设定当月的总天数 sum(总和)
            int sum =0;
            if (month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12) sum=31;
            else if (month==4 || month==6 || month==9 || month==11) sum=30;
            else if (month==2)sum = year%400==0 || year%4==0&&year%100!=0?29:28;
            //校验日
            if (sun>0&&sun<=sum)break;else System.out.println(year+"年"+month+"月一共才有"+sum+"日,您输入的天数有误请重新输入:");
        }
		//关闭资源
        sc.close();
        
        //todo 计算距离2000.1.1日过了day天
        //从2000年1月1日开始三天打鱼,两天晒网 -> 到键盘录入的时间
        //定义一共经过的天数
        int day = 0;
        //1、判断是否满足过一年如满足则看是否+365/+366
        for (int i=2000;i<year;i++){
            if (i%400==0 || i%4==0&&i%100!=0) day+=366;else day+=365;
        }
        //2、不足一年的加月份
        /* 平年的2月份有28天,闰年的2月份有29天。
            1月、3月、5月、7月、8月、10月、12月有31天,
            4月、6月、9月、11月有30天。*/
        for (int i=1;i<month;i++){
            if (i==4 || i==6 || i==9 || i==11) day+=30;
            else if (i==2) day+=(year%400==0 || year%4==0&&year%100!=0)?29:28;
            else day+=31;
        }
        //3、加不足一月的天数
        day += sun;
        //输出键盘录入的日期
        System.out.print(year+"年"+month+"月"+sun+"日 -> ");

        //todo 三天打鱼,两天晒网 ->输出是否打鱼
        int num = day % 7;
        if (num==4 || num==5) System.out.println("筛网");else System.out.println("打鱼");
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值