Three days of fishing two days of drying nets

There is a saying, an old man who lives in the river, fishing for three days, two days drying nets, asked since January 1, 2010, after a day of fishing is still drying nets.
First of all, this issue has to implement three important points to note
1.a years b c May to January 1, 2010, how many days
2. Calculate the number of days with poor cycle remainder
3. The remainder is 1,2,3 , the fishing; the remainder is 0,4, then drying nets

Flowchart is as follows:
Here Insert Picture Description

One problem: how poor seeking a few days of it?
When it comes to the number of days, if the user enters the number of days prior to January 1, 2010, and must have appropriate treatment, followed by a small rookie of the program.

if(year < 0||month < 1||month >12){
            System.out.println("请输入合法日期");
            System.exit(0);
        }else{
            if(isLeapYear(year)&&month == 2){
                Month_Days[month] = 29;
            }
            if(day == 0||day > Month_Days[month]){
                System.out.println("请输入合法日期");
                System.exit(0);
            }
        }

The difference between the number of days of solving process:
leap year, leap year problem, a leap year has 366 days, average year has 365 days a year, that is a leap year, February has 29 days
1 year: a year -2010-1: calculate the number of days throughout the year, which should pay attention leap year
2 months: if a and b is a leap year month number is greater than 2, added an extra day to
3 days: the days of c input
formula: number of days difference = year + month + day
small rookie of the code below it
1 . leap year judgment program

/**
     * @param: [year]
     * @return: boolean
     * @Description:判断year是否是闰年
     */
    public boolean isLeapYear(int year){

        if(year%400 == 0||(year%4 == 0&&year%100 != 0)){
            return true;
        }else{
            return false;
        }
    }

2. Get the number of days difference between the code:

/**
     * @param: []
     * @return: int
     * @Description:获取输入日期至2010年1月1日的天数差
     */
    public int getAllDays(){

        int days = 0;
        int startYear = 2010;
        if(year > startYear){
            for(;startYear<year;startYear++){
                if(isLeapYear(startYear)){
                    days += 366;
                }else{
                    days += 365;
                }
            }
        }
        for(int i=1;i<month;i++) {
            days += Month_Days[i];
            if(isLeapYear(year)&&month > 2){
                days++;
            }
        }
        days += day;
        return days;
    }

Question two: get a few bad days, the next step is to determine fishing nets drying Or friends. . .
Days 5% difference = 1 or 2 or 3---------> fishing
days 5% difference = 0 or 4 ----------------> drying network
connection down is the code that implements
this code there are ways shown in the oh out.txt

/**
     * @param: []
     * @return: void
     * @Description:判断这天是打渔Or晒网,并在out.txt中显示
     */
    public void isFishing() throws IOException{
        FileWriter writer = new FileWriter("out.txt");
        BufferedWriter bw = new BufferedWriter(writer);
            int result = getAllDays() % 5;
            if (result == 0 || result == 4) {
                System.out.println("晒网");
                bw.write("晒网");
            } else {
                System.out.println("打渔");
                bw.write("打渔");
            }
        bw.close();
    }

Question three: read the program from the date of the in.txt go?
I look Ha, Da Dada, here:
read from in.txt, a converted character data to integer data
Integer.parseInt (String) String role is to convert the data to a character type Integer integer data.

        BufferedReader br = new BufferedReader(new FileReader("in.txt"));
        String line = br.readLine();
        year = Integer.parseInt(line.substring(0, 4));
        month = Integer.parseInt(line.substring(4, 6));
        day = Integer.parseInt(line.substring(6, 8));

The basic problem of the end, put a test pattern
holds a valid date
Here Insert Picture Description
show results: Here Insert Picture Description
However, the program still has a problem, is that when you wrap in in.txt enter two dates, out.txt will only show the first date of doing things
for this problem looked up some code still not been resolved, off I will continue to learn, so the next time when more blog, this problem will continue to improve.

Guess you like

Origin blog.csdn.net/weixin_44369212/article/details/88078466
Recommended