有人可以帮我解决这个问题吗? (用java解决2000年)

时间:2013-05-24 18:35:00

标签: java

我在学校有一个用java解决2000年的项目。这是作业的描述:

使用以下内容创建Date对象: attribute:一个julian日期,表示自1501年1月1日(此项目的纪元日期)以来的天数 - 这应该是具有相关访问者和mutator方法的类的私有成员变量。 常数:每个月的合适常数                     适合于纪元日期的每个组成部分 方法:将给定年,月和日转换为朱利安日期的日期(长年,长月,长日)。 method(方法):将年,月和日转换为项目的纪元日期以来的天数的方法。 method:确定给定年份是否为闰年的方法 method:返回给定年份的天数的方法;使用条件运算符(?:)用单个语句替换当前的简单if语句 method:返回给定年份给定月份的天数的方法;方法应该使用适当的常量实现switch语句。 方法:也可能包括在课堂上开发的任何其他方法。 method:returnYear()一个方法,用于确定julian日期值的year组件 method:returnMonth()一个方法,用于确定julian日期值的月份组件 method:returnDay()一个方法,用于确定julian日期值的day组件 方法:returnMonthName()返回给定月份名称的方法(如果month = JAN返回“January”(使用switch语句)) method:returnDate()一个方法,以monthName day,year格式返回日期 class:包含两个查询方法的实用程序。

这是我到目前为止提供的代码..它没有编译,因为切换有问题!

import java.util.Scanner;

class Date
{

    public final static long EPOCHYEAR = 1501;
    private long julianDate;

    Date(long year, long month, long day)
    {

        julianDate = returnTotalJulianDay(year, month, day);
        System.out.println("Days is " + julianDate);
    }

    boolean isLeapYear(long year)
    {
        boolean answer;

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

        return answer;
    }

    long returnDaysInMonth(long year, long month)
    {

        if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
        {
            return 31;
        }
        else if(month == 4 || month == 6 || month == 9 || month == 11)
        {
            return 30;
        }
        else if(isLeapYear(year))
        {
            return 29;
        }
        else
        {
            return 28;
        }
    }

    long returnJulianDate(long year, long month, long day)
    {
        long julianDate;
        long monthCounter;

        julianDate = 0;
        monthCounter = 1;

        while(monthCounter < month)
        {

            julianDate += returnDaysInMonth(year, monthCounter);

            monthCounter += 1;

        }

        julianDate += day;

        return julianDate;
    }

    long returnTotalJulianDay(long year, long month, long day)
    {
        long totalJulianDay = 0;
        long yearCounter = 1;

        while(yearCounter < year)
        {

            totalJulianDay += returnJulianDate(year, month, yearCounter);

            yearCounter += 1;

        }

        return totalJulianDay;
    }

    long returnDaysInYear(long year)
    {
        final long DAYSINYEAR = 365;

        if(isLeapYear(year))
        {
            return 366;
        }
        else
        {
            return DAYSINYEAR;
        }
    }

    long returnJulianEpochDays(long year, long month, long day)
    {

        long yearCounter = EPOCHYEAR;
        long total = 0;

        while(yearCounter < year)
        {

            total += returnDaysInYear(yearCounter);

            yearCounter += 1;
        }

        total += returnJulianDate(year, month, day);

        return total;
    }

    long returnYear()
    {
        long dayCounter = 0;
        long yearCounter = EPOCHYEAR;

        for(dayCounter = this.julianDate; dayCounter > returnDaysInYear(yearCounter); yearCounter++)
        {
            dayCounter -= returnDaysInYear(yearCounter);
        }

        return yearCounter;
    }

    long returnMonth()
    {
        long julianEpochDays = julianDate;
        long yearCounter = EPOCHYEAR;
        long monthCounter = 1;

        while(julianEpochDays > returnDaysInYear(yearCounter))
        {
            julianEpochDays -= returnDaysInYear(yearCounter);
            yearCounter++;

        }

        while(julianEpochDays > returnDaysInMonth(yearCounter, monthCounter))
        {
            julianEpochDays -= returnDaysInMonth(yearCounter, monthCounter);
            monthCounter++;

        }

        return monthCounter;
    }

    long returnDay()
    {
        long julianEpochDays = julianDate;
        long yearCounter = EPOCHYEAR;
        long monthCounter = 1;

        while(julianEpochDays > returnDaysInYear(yearCounter))
        {
            julianEpochDays -= returnDaysInYear(yearCounter);
            yearCounter++;

        }

        while(julianEpochDays > returnDaysInMonth(yearCounter, monthCounter))
        {
            julianEpochDays -= returnDaysInMonth(yearCounter, monthCounter);
            monthCounter++;

        }

        return julianEpochDays;
    }

    long returnMonthName()
    {
        int month = 0;

        final int JAN = 1;
        final int FEB = 2;
        final int MAR = 3;
        final int APR = 4;
        final int MAY = 5;
        final int JUN = 6;
        final int JUL = 7;
        final int AUG = 8;
        final int SEP = 9;
        final int OCT = 10;
        final int NOV = 11;
        final int DEC = 12;

        switch(month)
        {

            case JAN:
                return "January";
            case FEB:
                return "Febuary";
            case MAR:
                return "March";
            case APR:
                return "April";
            case MAY:
                return "May";
            case JUN:
                return "June";
            case JUL:
                return "July";
            case AUG:
                return "August";
            case SEP:
                return "September";
            case OCT:
                return "October";
            case NOV:
                return "November";
            case DEC:
                return "December";

        }
    }
}

class utility
{

    public char queryForCharacter(String prompt)
    {
        int typedCharacter = ' ';

        try
        {
            System.out.print(prompt);
            typedCharacter = System.in.read();
        }
        catch(Exception e)
        {
        }

        return (char) typedCharacter;
    }

    public static long queryForLong(String prompt)
    {
        Scanner keyboard;
        long theNumber;

        keyboard = new Scanner(System.in);
        System.out.print(prompt);
        theNumber = keyboard.nextInt();

        return theNumber;
    }
}

public class DateDriver
{

    public static void main(String[] args)
    {
        Date aTestDate = null;
        aTestDate = new Date(1502, 1, 1);
    }
}

2 个答案:

答案 0 :(得分:2)

您的方法returnMonthName()被声明为返回long,但switch case返回Strings。

此外,如果switch中没有一个案例匹配,该方法需要返回一些内容。

答案 1 :(得分:0)

您的函数返回long类型:

“long returnMonthName(){”

但是你正在返回一种“字符串”。即“一月”。