我怎样才能简化我的代码?
How can I simplify my code?
我最近写了一个程序来计算 1900 年之后的第 13 个星期五。代码非常好并且可以工作;但是有些部分非常多余。反正有没有我可以简化它。
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);
}
}
好吧,一方面,您的代码没有达到您声明的目标。无论如何,简短的回答是使用 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;
}
大大改进代码的最简单方法是使用数组。此外,您必须了解 if
语句的逻辑才能压缩它们。为了可读性,当你想说days in a week
时不要使用7
,当你想说years in a month
时不要使用12
。 创建具有明确名称的常量并在任何需要的地方使用它们。
下面是您重构第一步的开始。检查这个简化的代码是否仍然有效,并用它来进一步改进它(更好的名字,更明确的逻辑 incrementForMonth
...)。
public class friday {
private static final int DAYS_IN_WEEK = 7;
private static final int MONTHS_IN_YEAR = 12;
private static void updateDays(int[] days, int day) {
checkIsInRange(0, year, DAYS_IN_WEEK);
days[(day + 4) % DAYS_IN_WEEK]++;
}
private static boolean isLeapYear(int year) {
checkIsInRange(0, year, MONTHS_IN_YEAR);
return (year % 4 == 0) && (year % 100 != 0 || year % 400 == 0);
}
private static void checkIsInRange(int inclusiveLowerBound, int toTest, int exclusiveUpperBound) {
if (toTest < inclusiveLowerBound || exclusiveUpperBound <= toTest) throw new IllegalArgumentException(toTest + " not in [" + inclusiveLowerBound + ", " + exclusiveUpperBound + "]");
}
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 month = 1;
int day = 1;
int months = MONTHS_IN_YEAR*years;
int test = 0;
int year = 1900;
int days = int[DAYS_IN_WEEK];
int[] incrementForMonth = { 3, 3, 1, 3, 2, 3, 2, 3, 3, 2, 3, 2 };
for(int i = 0; i < months; i++) {
test = isLeapYear(year) ? 1 : 0;
updateDays(days, day);
if (month != 2 || test == 1) {
day = (day + incrementForMonth[month]) % DAYS_IN_WEEK;
}
if (month == 0) {
year++;
}
}
month = (month + 1) % MONTHS_IN_YEAR;
}
pw.println(saturday + " " + sunday + " " + monday + " " + tuesday + " " + wedensday+ " " + thursday + " " + friday);
pw.close();
}
我最近写了一个程序来计算 1900 年之后的第 13 个星期五。代码非常好并且可以工作;但是有些部分非常多余。反正有没有我可以简化它。
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);
}
}
好吧,一方面,您的代码没有达到您声明的目标。无论如何,简短的回答是使用 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;
}
大大改进代码的最简单方法是使用数组。此外,您必须了解 if
语句的逻辑才能压缩它们。为了可读性,当你想说days in a week
时不要使用7
,当你想说years in a month
时不要使用12
。 创建具有明确名称的常量并在任何需要的地方使用它们。
下面是您重构第一步的开始。检查这个简化的代码是否仍然有效,并用它来进一步改进它(更好的名字,更明确的逻辑 incrementForMonth
...)。
public class friday {
private static final int DAYS_IN_WEEK = 7;
private static final int MONTHS_IN_YEAR = 12;
private static void updateDays(int[] days, int day) {
checkIsInRange(0, year, DAYS_IN_WEEK);
days[(day + 4) % DAYS_IN_WEEK]++;
}
private static boolean isLeapYear(int year) {
checkIsInRange(0, year, MONTHS_IN_YEAR);
return (year % 4 == 0) && (year % 100 != 0 || year % 400 == 0);
}
private static void checkIsInRange(int inclusiveLowerBound, int toTest, int exclusiveUpperBound) {
if (toTest < inclusiveLowerBound || exclusiveUpperBound <= toTest) throw new IllegalArgumentException(toTest + " not in [" + inclusiveLowerBound + ", " + exclusiveUpperBound + "]");
}
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 month = 1;
int day = 1;
int months = MONTHS_IN_YEAR*years;
int test = 0;
int year = 1900;
int days = int[DAYS_IN_WEEK];
int[] incrementForMonth = { 3, 3, 1, 3, 2, 3, 2, 3, 3, 2, 3, 2 };
for(int i = 0; i < months; i++) {
test = isLeapYear(year) ? 1 : 0;
updateDays(days, day);
if (month != 2 || test == 1) {
day = (day + incrementForMonth[month]) % DAYS_IN_WEEK;
}
if (month == 0) {
year++;
}
}
month = (month + 1) % MONTHS_IN_YEAR;
}
pw.println(saturday + " " + sunday + " " + monday + " " + tuesday + " " + wedensday+ " " + thursday + " " + friday);
pw.close();
}