使用ifelse显示工作日

时间:2016-02-13 16:49:59

标签: java if-statement calendar

我正在编写此代码以通过使用if else语句从main函数调用它来显示工作日,但我无法显示星期一之后的日期。似乎没有调用if-else语句的其余部分。请帮忙..

public class WeekdayName {

    public static String weekday_name(int weekday) {
        String result = "";

        if ( weekday == 1 ) {
            result = "Sunday";
        } else if ( weekday == 2 ) {
            result = "Monday";
        } else if ( weekday == 3 ) {
            result = "Tuesday";
        } else if ( weekday == 4 ) {
            result = "Wednesday";
        } else if ( weekday == 5 ) {
            result = "Thursday";
        } else if ( weekday == 6 ) {
            result = "Friday";
        } else if ( weekday == 7 ) {
            result = "Saturday";
        } else if ( weekday == 0 ) {
            result = "Sunday";
        } else {
            result = "ERROR";
        }
        return result;
    }

    public static void main( String[] args ) {

        System.out.println( "Weekday 1: " + weekday_name(1) );
        System.out.println( "Weekday 2: " + weekday_name(2) );
        System.out.println( "Weekday 3: " + weekday_name(3) );
        System.out.println( "Weekday 4: " + weekday_name(4) );
        System.out.println( "Weekday 5: " + weekday_name(5) );
        System.out.println( "Weekday 6: " + weekday_name(6) );
        System.out.println( "Weekday 7: " + weekday_name(7) );
        System.out.println( "Weekday 0: " + weekday_name(0) );
        System.out.println();
        System.out.println( "Weekday 43: " + weekday_name(43) );
        System.out.println( "Weekday 17: " + weekday_name(17) );
        System.out.println( "Weekday -1: " + weekday_name(-1) );

        Calender calender = new GregorianCalender();
        int dayofWeek = calender.get(Calender.DAY_OF_WEEK);

        System.out.println( "\nToday is a " + weekday_name(dayofWeek) + "!" );
    }

}

请帮助...

5 个答案:

答案 0 :(得分:1)

1。你有错字。它的Calendar不是Calender

2。输出为:

Weekday 1: Sunday
Weekday 2: Monday
Weekday 3: Tuesday
Weekday 4: Wednesday
Weekday 5: Thursday
Weekday 6: Friday
Weekday 7: Saturday
Weekday 0: Sunday

Weekday 43: ERROR
Weekday 17: ERROR
Weekday -1: ERROR

Today is a Saturday!

所以它似乎对我有用。

3。您最好使用switch语句。它看起来更短更优雅:

public static String weekday_name( int weekday )
{
    switch (weekday) {
        case 1: return "Sunday";
        case 2: return "Monday";
        case 3: return "Tuesday";
        case 4: return "Wednesday";
        case 5: return "Thursday";
        case 6: return "Friday";
        case 7: return "Saturday";
        case 0: return "Sunday";
        default: return "ERROR";
    }
}

输出也一样:)

答案 1 :(得分:0)

由于两个问题,您当前的代码无法编译:

  • 它是Calendar,而不是Calender
  • 您还需要导入java.util.Calendar

您之前只看到星期一输出的原因可能是您在不知情的情况下执行了一个旧的(正确编译的)源版本,但尚未包含其他日期的输出。但那只是猜测。

请注意,DAY_OF_WEEK实际上并未返回0,因此您也可以考虑为该案例返回ERROR

固定代码:

import java.util.GregorianCalendar;
import java.util.Calendar;

public class WeekdayName {
    public static String weekday_name(int weekday) {
        String result = "";

        if (weekday == 1) {
            result = "Sunday";
        } else if (weekday == 2) {
            result = "Monday";
        } else if (weekday == 3) {
            result = "Tuesday";
        } else if (weekday == 4) {
            result = "Wednesday";
        } else if (weekday == 5) {
            result = "Thursday";
        } else if (weekday == 6) {
            result = "Friday";
        } else if (weekday == 7) {
            result = "Saturday";
        } else if (weekday == 0) {
            result = "Sunday";
        } else {
            result = "ERROR";
        }
        return result;
    }

    public static void main(String[] args) {
        System.out.println("Weekday 1: " + weekday_name(1));
        System.out.println("Weekday 2: " + weekday_name(2));
        System.out.println("Weekday 3: " + weekday_name(3));
        System.out.println("Weekday 4: " + weekday_name(4));
        System.out.println("Weekday 5: " + weekday_name(5));
        System.out.println("Weekday 6: " + weekday_name(6));
        System.out.println("Weekday 7: " + weekday_name(7));
        System.out.println("Weekday 0: " + weekday_name(0));
        System.out.println();
        System.out.println("Weekday 43: " + weekday_name(43));
        System.out.println("Weekday 17: " + weekday_name(17));
        System.out.println("Weekday -1: " + weekday_name(-1));

        Calendar calender = new GregorianCalendar();
        int dayofWeek = calender.get(Calendar.DAY_OF_WEEK);

        System.out.println("\nToday is a " + weekday_name(dayofWeek) + "!");
    }
}

之后,您的逻辑似乎工作正常:

Weekday 1: Sunday
Weekday 2: Monday
Weekday 3: Tuesday
Weekday 4: Wednesday
Weekday 5: Thursday
Weekday 6: Friday
Weekday 7: Saturday
Weekday 0: Sunday

Weekday 43: ERROR
Weekday 17: ERROR
Weekday -1: ERROR

Today is a Saturday!

答案 2 :(得分:0)

  

你的错误只是拼写GregorianCalender   应为GregorianCalendarCalender   Calendar

     

您需要在代码中import java.util.Calendar;

import java.util.Calendar;
    import java.util.GregorianCalendar;

    public class WeekdayNAme
    {
        public static String weekday_name( int weekday )
        {
            String result = "";

            if ( weekday == 1 )
            {
                result = "Sunday";
            }
            else if ( weekday == 2 )
            {
                result = "Monday";
            }
            else if ( weekday == 3 )
            {
                result = "Tuesday";
            }
            else if ( weekday == 4 )
            {
                result = "Wednesday";
            }

            else if ( weekday == 5 )
            {
                result = "Thursday";
            }
            else if ( weekday == 6 )
            {
                result = "Friday";
            }

            else if ( weekday == 7 )
            {
                result = "Saturday";
            }
            else if ( weekday == 0 )
            {
                result = "Sunday";
            }
            else
            {
                result = "ERROR";
            }
            return result;
        }


        public static void main( String[] args )
        {
            System.out.println( "Weekday 1: " + weekday_name(1) );
            System.out.println( "Weekday 2: " + weekday_name(2) );
            System.out.println( "Weekday 3: " + weekday_name(3) );
            System.out.println( "Weekday 4: " + weekday_name(4) );
            System.out.println( "Weekday 5: " + weekday_name(5) );
            System.out.println( "Weekday 6: " + weekday_name(6) );
            System.out.println( "Weekday 7: " + weekday_name(7) );
            System.out.println( "Weekday 0: " + weekday_name(0) );
            System.out.println();
            System.out.println( "Weekday 43: " + weekday_name(43) );
            System.out.println( "Weekday 17: " + weekday_name(17) );
            System.out.println( "Weekday -1: " + weekday_name(-1) );

            Calendar calender = new GregorianCalendar();
            int dayofWeek = calender.get(Calendar.DAY_OF_WEEK);

            System.out.println( "\nToday is a " + weekday_name(dayofWeek) + "!" );
        }

    }
  

之后它将产生流动的输出:

Weekday 1: Sunday
Weekday 2: Monday
Weekday 3: Tuesday
Weekday 4: Wednesday
Weekday 5: Thursday
Weekday 6: Friday
Weekday 7: Saturday
Weekday 0: Sunday

Weekday 43: ERROR
Weekday 17: ERROR
Weekday -1: ERROR

Today is a Saturday!

答案 3 :(得分:0)

你有一个类

的拼写错误
Calender calender = new GregorianCalender();
int dayofWeek = calender.get(Calender.DAY_OF_WEEK);

必须是

Calendar calender = new GregorianCalendar();
int dayofWeek = calender.get(Calendar.DAY_OF_WEEK);

答案 4 :(得分:0)

不是100%相关,但枚举在这里提供了一个优雅的解决方案......

private enum WeekDay {
    SUNDAY,
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY;

    static WeekDay fromDayNumber(int dayNumber) {
        return WeekDay.values()[dayNumber -1];
    }
}

...        Calendar calender = new GregorianCalendar();         int dayofWeek = calender.get(Calendar.DAY_OF_WEEK);

    System.out.println( "\nToday is a " + WeekDay.fromDayNumber(dayofWeek) + "!" );

...