三天打鱼两天晒网文件输入输出java

源代码附上

/* author:.。。
 * title:三天打鱼打鱼两天晒网
 * data: 2019-3-1
 * */
//特别注意
//特别注意
//特别注意
//重要的事情说三遍
/*要想运行程序请在D盘下新建一个名为in.txt ,并将数据写入
格式为
\20150203
20130104
20120406
20030406
然后其重命名为in.dat文件和新建一个out.txt的文本
 * 文档
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
  
import java.io.PrintStream; 


public class Test {

	public static void main(String[] args) throws IOException {
		
		File file =new File("D:\\in.dat");//打开D盘下的in.dat文件
		if(file.exists()){
			
			InputStream input=new FileInputStream(file);
			PrintStream ps = new PrintStream("D:\\out.txt"); //输出至d盘下out.txt
			System.setOut(ps);
			byte[] data=new byte[1024];
			int len=input.read(data);//读取字符长度
			int a=len/10;//读取行数
		
			System.out.println("数据中输入的数据为");
			System.out.println(new String(data,0,len));//打印数据
			for(int i=0;i<=a;i++)//对每一行的数据进型分解赋值
			{   
			System.out.printf("第%d个数据测试为",i+1);
		    System.out.println("");
			System.out.println(new String(data,10*i,4)+"年");
			System.out.println(new String(data,10*i+4,2)+"月");
			System.out.println(new String(data,10*i+6,2)+"日");
			String syear=new String(data,10*i,4);//将第i+1行前四个字符赋给syear
			String smonth=new String(data,10*i+4,2);//将第i+1行第56个字符赋给smonth
			String sday=new String(data,10*i+6,2);//将第i+1行第78个字符给sday
			int year=Integer.parseInt(syear);//   强制类型转换
			int month=Integer.parseInt(smonth);
			int day=Integer.parseInt(sday);
			 hefa(year, month, day);              //判定是否合法输出语句
			 if(xianshi(year, month, day))          //合法后显示最终结果
	               { 
				     OUT(AllDays(year, month, day));
	               }
			 }
			 input.close();
	}

}
	public static void hefa(int year,int month,int day)  //判断合法,如果不合法输出提示
    {
   	 if(year<2010||month>12||day>31||month<0||day<0)
   	 {  System.out.println("输入数据不合法");}           //先将最不可能的数据剔除
   	 else
   	 {
   	 if(month==4||month==6||month==9||month==11)      //再通过月份进行分析
   	 {
   		 if(day>30)
   		 {
   			 System.out.println("输入数据不合法"); 
   		 }
   	 }
   	 if(month==2)       //2月是最特殊的月份,它的天数和年有关
   	 {
   		 if(day>29)
   		 {
   			 System.out.println("输入数据不合法"); 
   		 }else
   		 {
   			 if(runNian(year))//判断是否为润年
   			 {}
   			 else
   			 {
   				 if(day>28)//非润年就不合法
   				 {
   					 System.out.println("输入数据不合法"); 
   				 }
   			 }
   		 }
   	 }
   	 }
    }
    public static boolean xianshi(int year,int month,int day)//控制输出的显示函数,数据不合法就不能输出
    {
   	 if(year<2010||month>12||day>31||month<0||day<0)//将最不合法的数据返回
   	 {  return false;}
   	 else                            
   	 {
   	 if(month==4||month==6||month==9||month==11) //再根据月份进行分析
   	 {
   		 if(day>30)
   		 {
   			 return false; 
   		 }return true;
   	 }
   	 if(month==2)  //同样的将二月份单独拿出分析
   	 {
   		 if(day>29)
   		 {
   			 return false; 
   		 }else
   		 {
   			 if(runNian(year))
   			 {return true;}//是润年就有29天,返回真值
   			 else
   			 {
   				 if(day>28)
   				 {
   					 return false;//非闰年大于28天返回假值
   				 }return true;
   			 }
   		 }
   	 }return true;//大月份(31天)在这返回真值
   	 }
    }
   
    public static void OUT(int days) // 判断函数判断在所有天数过去后打鱼还是晒网
    {   
        int x = days % 5;
        if (x >= 1 && x <= 3) {
            System.out.println("小老板,今天该打鱼啊");
        } else if (x == 4 || x == 0) {
            System.out.println("小老板,今天该 晒网啊");
        }
    }

    
    public static boolean runNian(int year) // 判断是否是闰年
    {
        if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
            return true;
        }
        return false;
    }

    
    public static int AllDays(int year, int month, int day)// 计算距离输入日期多少天
    {
        int sum = 0;
        // 计算2010--(year-1)之间有多少天
        for (int i = 2010; i < year; i++) {
            if (runNian(i))   //是润年就加366天
            {
                sum =sum + 366;
            } else
            {
                sum =sum + 365;  //非润年就加365天
            }
        }

       
        sum =sum + bennianDays(year, month, day); // 计算year内多少天
        return sum;
    }

    public static int monthDays(int year, int month) // 判断每月有几天
    {
        int days = 0;
        switch (month) {
        case 1:days = 31;
        case 3:days = 31;
        case 5:days = 31;
        case 7:days = 31;
        case 8:days = 31;
        case 10:days = 31;
        case 12:days = 31;
            break;
        case 4:days = 30;
        case 6:days = 30;
        case 9:days = 30;
        case 11:days = 30;
             break;
        case 2:
            if (runNian(year))
            {
                days = 29;
            } else {
                days = 28;
            }
            break;
        }

        return days;
    }

  
    public static int bennianDays(int year, int month, int day)// 计算该年内共有多少天
    {
        int sum = 0;
        for (int i = 1; i < month; i++) {
            sum += monthDays(year, i);
        }
        return sum + day;
    }

   
}


运行结果
在这里插入图片描述
在这里插入图片描述

代码参考:https://blog.csdn.net/weixin_42391433/article/details/80582184

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值