如何简化我的代码?

时间:2015-08-14 19:39:48

标签: java algorithm simplify

我最近编写了一个程序来计算1900年以后的第十三个星期五的数量。代码非常精细且有效;但是有些部分非常多余。无论如何我可以简化它。

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class friday {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("friday.in"));
        PrintWriter pw= new PrintWriter(new FileWriter("friday.out"));
        int years = Integer.parseInt(br.readLine());
        int saturday = 0;
        int sunday = 0;
        int monday = 0;
        int tuesday = 0;
        int wedensday = 0;
        int thursday = 0;
        int friday = 0;
        int month = 1;
        int day = 1;
        int months = 12*years;
        int test = 0;
        int year = 1900;

        for(int i = 0; i < months; i++)
        {
            if ((year % 4 == 0) && year % 100 != 0)
            {
                test = 1;
            }
            else if ((year % 4 == 0) && (year % 100 == 0) && (year % 400 == 0))
            {
                test = 1;
            }
            else
            {
                test = 0;
            }

        if(month == 1)
        {
            if(day == 1)
            {
                saturday++;
            }
            if(day == 2)
            {
                sunday++;
            }
            if(day == 3)
            {
                monday++;
            }
            if(day == 4)
            {
                tuesday++;
            }
            if(day == 5)
            {
                wedensday++;
            }
            if(day == 6)
            {
                thursday++;
            }
            if(day == 0)
            {
                friday++;
            }
            day = (day + 3) % 7;
        }
        if(month == 2)
        {
            if(test == 1)
            {
                if(day == 1)
                {
                    saturday++;
                }
                if(day == 2)
                {
                    sunday++;
                }
                if(day == 3)
                {
                    monday++;
                }
                if(day == 4)
                {
                    tuesday++;
                }
                if(day == 5)
                {
                    wedensday++;
                }
                if(day == 6)
                {
                    thursday++;
                }
                if(day == 0)
                {
                    friday++;
                }
                //leapyear = 1;
                day = (day + 1) % 7;
            }
            else
            {
                if(day == 1)
                {
                    saturday++;
                }
                if(day == 2)
                {
                    sunday++;
                }
                if(day == 3)
                {
                    monday++;
                }
                if(day == 4)
                {
                    tuesday++;
                }
                if(day == 5)
                {
                    wedensday++;
                }
                if(day == 6)
                {
                    thursday++;
                }
                if(day == 0)
                {
                    friday++;
                }
                day = (day + 0) % 7;
                //leapyear++;
            }
        }
        if(month == 3)
        {
            if(day == 1)
            {
                saturday++;
            }
            if(day == 2)
            {
                sunday++;
            }
            if(day == 3)
            {
                monday++;
            }
            if(day == 4)
            {
                tuesday++;
            }
            if(day == 5)
            {
                wedensday++;
            }
            if(day == 6)
            {
                thursday++;
            }
            if(day == 0)
            {
                friday++;
            }
            day = (day + 3) % 7;
        }
        if(month == 4)
        {
            if(day == 1)
            {
                saturday++;
            }
            if(day == 2)
            {
                sunday++;
            }
            if(day == 3)
            {
                monday++;
            }
            if(day == 4)
            {
                tuesday++;
            }
            if(day == 5)
            {
                wedensday++;
            }
            if(day == 6)
            {
                thursday++;
            }
            if(day == 0)
            {
                friday++;
            }
            day = (day + 2) % 7;
        }
        if(month == 5)
        {
            if(day == 1)
            {
                saturday++;
            }
            if(day == 2)
            {
                sunday++;
            }
            if(day == 3)
            {
                monday++;
            }
            if(day == 4)
            {
                tuesday++;
            }
            if(day == 5)
            {
                wedensday++;
            }
            if(day == 6)
            {
                thursday++;
            }
            if(day == 0)
            {
                friday++;
            }
            day = (day + 3) % 7;
        }
        if(month == 6)
        {
            if(day == 1)
            {
                saturday++;
            }
            if(day == 2)
            {
                sunday++;
            }
            if(day == 3)
            {
                monday++;
            }
            if(day == 4)
            {
                tuesday++;
            }
            if(day == 5)
            {
                wedensday++;
            }
            if(day == 6)
            {
                thursday++;
            }
            if(day == 0)
            {
                friday++;
            }
            day = (day + 2) % 7;
        }
        if(month == 7)
        {
            if(day == 1)
            {
                saturday++;
            }
            if(day == 2)
            {
                sunday++;
            }
            if(day == 3)
            {
                monday++;
            }
            if(day == 4)
            {
                tuesday++;
            }
            if(day == 5)
            {
                wedensday++;
            }
            if(day == 6)
            {
                thursday++;
            }
            if(day == 0)
            {
                friday++;
            }
            day = (day + 3) % 7;
        }
        if(month == 8)
        {
            if(day == 1)
            {
                saturday++;
            }
            if(day == 2)
            {
                sunday++;
            }
            if(day == 3)
            {
                monday++;
            }
            if(day == 4)
            {
                tuesday++;
            }
            if(day == 5)
            {
                wedensday++;
            }
            if(day == 6)
            {
                thursday++;
            }
            if(day == 0)
            {
                friday++;
            }
            day = (day + 3) % 7;
        }
        if(month == 9)
        {
            if(day == 1)
            {
                saturday++;
            }
            if(day == 2)
            {
                sunday++;
            }
            if(day == 3)
            {
                monday++;
            }
            if(day == 4)
            {
                tuesday++;
            }
            if(day == 5)
            {
                wedensday++;
            }
            if(day == 6)
            {
                thursday++;
            }
            if(day == 0)
            {
                friday++;
            }
            day = (day + 2) % 7;
        }
        if(month == 10)
        {
            if(day == 1)
            {
                saturday++;
            }
            if(day == 2)
            {
                sunday++;
            }
            if(day == 3)
            {
                monday++;
            }
            if(day == 4)
            {
                tuesday++;
            }
            if(day == 5)
            {
                wedensday++;
            }
            if(day == 6)
            {
                thursday++;
            }
            if(day == 0)
            {
                friday++;
            }
            day = (day + 3) % 7;
        }
        if(month == 11)
        {
            if(day == 1)
            {
                saturday++;
            }
            if(day == 2)
            {
                sunday++;
            }
            if(day == 3)
            {
                monday++;
            }
            if(day == 4)
            {
                tuesday++;
            }
            if(day == 5)
            {
                wedensday++;
            }
            if(day == 6)
            {
                thursday++;
            }
            if(day == 0)
            {
                friday++;
            }
            day = (day + 2) % 7;
        }
        if(month == 0)
        {
            if(day == 1)
            {
                saturday++;
            }
            if(day == 2)
            {
                sunday++;
            }
            if(day == 3)
            {
                monday++;
            }
            if(day == 4)
            {
                tuesday++;
            }
            if(day == 5)
            {
                wedensday++;
            }
            if(day == 6)
            {
                thursday++;
            }
            if(day == 0)
            {
                friday++;
            }
            day = (day + 3) % 7;
            year++;
        }

        month = (month + 1) % 12;


    }
        pw.println(saturday + " " + sunday + " " + monday + " " + tuesday + " " + wedensday+ " " + thursday + " " + friday);
        pw.close();
        //System.out.println(saturday + " " + sunday + " " + monday + " " + tuesday + " " + wedensday+ " " + thursday + " " + friday);
    }
}

1 个答案:

答案 0 :(得分:0)

嗯,首先,你的代码并没有达到你所说的目标。无论如何,简短的回答是使用Calendar对象。

如果你只想计算你所说的星期五13号的数字,这里是代码:

public static int countFriday13(int years) {
    Calendar cal = Calendar.getInstance();
    cal.clear();
    cal.set(1900, Calendar.JANUARY, 13);
    int count = 0;
    for (int i = 0; i < years * 12; i++) {
        if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY)
            count++;
        cal.add(Calendar.MONTH, 1);
    }
    return count;
}