工作日计算器逻辑错误 - Java

时间:2016-03-21 04:49:20

标签: java methods

我是一名自学Java的初学者。我刚学会了如何从方法中返回值。我尝试了这个练习 Weekday Calculator

我有工作代码,花了我一段时间才写。 (#proudNOOB)

import java.util.Scanner;

public class WeekdayCalculator {
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);

        int yearsSince1900, total, remainder;

        System.out.print("Enter your year of birth ~ ");
        int year = keyboard.nextInt();

        System.out.print("Enter the month you were born in " + year + " ~ ");
        int month = keyboard.nextInt();

        System.out.print("Enter the day you were born in " + monthName(month) + " ~ ");
        int day = keyboard.nextInt();

        yearsSince1900 = year - 1900;
        // System.out.println(yy);

        total = (yearsSince1900 / 4) + yearsSince1900 + day + monthOffset(month);
        if ((isLeap(year) == true) && month == 1 || month == 2) {
            total = total - 1;
        }
        // System.out.println(total);

        remainder = total % 7;
        // System.out.println(remainder);

        // Display (day of week, month, day, year)
        System.out.println("\nYou were born on " + weekdayName(remainder) + ", " + monthName(month) + " " + day + ", " + year + ".");
    }

    public static int monthOffset(int month) {
        int offset;

        if (month == 1) {
            offset = 1;
        } 
        else if (month == 2) {
            offset = 4;
        } 
        else if (month == 3) {
            offset = 4;
        } 
        else if (month == 4) {
            offset = 0;
        } 
        else if (month == 5) {
            offset = 2;
        } 
        else if (month == 6) {
            offset = 5;
        } 
        else if (month == 7) {
            offset = 0;
        } 
        else if (month == 8) {
            offset = 3;
        } 
        else if (month == 9) {
            offset = 6;
        } 
        else if (month == 10) {
            offset = 1;
        } 
        else if (month == 11) {
            offset = 4;
        } 
        else if (month == 12) {
            offset = 6;
        } 
        else {
            offset = -1;
        }

        return offset;
    }

    public static boolean isLeap(int year) {
        boolean leap;

        if (year % 400 == 0) {
            leap = true;
        } 
        else if (year % 100 == 0) {
            leap = false;
        } 
        else if (year % 4 == 0) {
            leap = true;
        } 
        else {
            leap = false;
        }

        return leap;
    }

    public static String weekdayName(int remainder) {
        String weekdayWord = "";

        if (remainder == 1) {
            weekdayWord = "Sunday";
        } 
        else if (remainder == 2) {
            weekdayWord = "Monday";
        } 
        else if (remainder == 3) {
            weekdayWord = "Tuesday";
        } 
        else if (remainder == 4) {
            weekdayWord = "Wednesday";
        } 
        else if (remainder == 5) {
            weekdayWord = "Thursday";
        } 
        else if (remainder == 6) {
            weekdayWord = "Friday";
        } 
        else if (remainder == 7) {
            weekdayWord = "Saturday";
        }

        return weekdayWord;
    }

    public static String monthName(int month) {
        String monthWord = "";

        if (month == 1) {
            monthWord = "January";
        } 
        else if (month == 2) {
            monthWord = "February";
        } 
        else if (month == 3) {
            monthWord = "March";
        } 
        else if (month == 4) {
            monthWord = "April";
        } 
        else if (month == 5) {
            monthWord = "May";
        } 
        else if (month == 6) {
            monthWord = "June";
        } 
        else if (month == 7) {
            monthWord = "July";
        } 
        else if (month == 8) {
            monthWord = "August";
        } 
        else if (month == 9) {
            monthWord = "September";
        } 
        else if (month == 10) {
            monthWord = "October";
        } 
        else if (month == 11) {
            monthWord = "November";
        } 
        else if (month == 12) {
            monthWord = "December";
        } 
        else {
            monthWord = "error";
        }

        return monthWord;
    }
}

我用练习说明中的自动测试测试了我的程序。该计划适用于所有那些测试,除了1977年的2 13 13,它给了我输出"你出生于1977年2月13日和#34;它也适用于我的生日,以及其他随机日期。但是,我尝试了我父亲的生日(仅仅是为了它)并且输出是错误的,就像测试用例一样(工作日名称缺失)。

INPUT:

输入您的出生年份〜1962年

输入您出生于1962〜12年的月份

输入您在12月〜29日出生的那天

输出:

你出生于1962年12月29日。

据我所知,根据我的输入和程序的逻辑,yearsSince1900变量的值为62.然后总变量保持((62/4)+ 62 + 29 + 6)等于112。然后112%7等于16,这意味着余数等于0.因此,weekdayName方法不返回weekdayWord,因为没有条件,其余= = 0.什么是修复这个逻辑错误的最佳方法搞砸了某些生日。我希望这个程序适用于我父亲的出生日期(以及1900年的任何出生日期),所以我可以向他展示。 :D此外,任何关于自学编程的提示以及在哪里找到像这样的简单游戏或练习都将受到高度赞赏。谢谢!

2 个答案:

答案 0 :(得分:0)

看起来这个bug可以通过将星期六的余数更改为零来解决,因为你的方法没有考虑模数为0。

else if (remainder == 0) {
    weekdayWord = "Saturday";
}

答案 1 :(得分:0)

感谢每个指导我调试逻辑错误的人。

首先,如果余数等于0且余数等于7,则应将daydayWord设置为“Saturday”

else if (remainder == 7 || remainder == 0) {
        weekdayWord = "Saturday";
    }

其次,我对闰年检查的条件,由于缺少括号,会影响总数,最终影响工作日。正确的条件是:

if ((isLeap(year) == true) && (month == 1 || month == 2)) {
        total = total - 1;
    }

该程序现在已成功适用于我尝试过的所有案例。是时候向我父亲展示了这一点。 :P