我是一位非常业余的编码人员,他似乎无法解决从用户输入的整数中获取星期几(例如星期日,星期一等)的问题。我只是想给用户一个消息,它的布局(mm / dd / yyyy,dayOfWeek),但是我没有改变一周中的某天,而是不断获取星期三作为一周中的某天的答案,无论我把什么放在里面提示框。我只需要一个新的方向。我的代码或我看不到的途径有任何错误吗?任何帮助将不胜感激。

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

        String returnValue = "";
        int month2=0;
        int day2=0;
        int year2=0;
        GregorianCalendar userbirthday = new GregorianCalendar(year2, month2, day2);
        int userweekday=userbirthday.get(Calendar.DAY_OF_WEEK);




        String usermonth;
        System.out.print ("What month is your birthday?");
        usermonth = user_input.next();

        String userday;
        System.out.print ("What day is your birthday?");
        userday = user_input.next();

        String useryear;
        System.out.print ("What year was your birth?");
        useryear = user_input.next();

        year2 = Integer.parseInt(useryear);
        month2 = Integer.parseInt(usermonth);
        day2 = Integer.parseInt(userday);


        String dayOfTheWeek = "";

        if(month2 == 0){
        System.out.println("That's not a valid birthday! Check your month.");
        System.exit(0);
    } else if (month2>=13){
       System.out.println("That's not a valid birthday! Check your month.");
       System.exit(0);
    }
        if(day2 == 0){
        System.out.println("That's not a valid birthday! Check your day.");
        System.exit(0);
    } else if (day2>=32){
       System.out.println("That's not a valid birthday! Check your day.");
       System.exit(0);
    }
         if(userweekday == 2){
         dayOfTheWeek= "Mon";
    } else if (userweekday==3){
        dayOfTheWeek = "Tue";
    } else if (userweekday==4){
        dayOfTheWeek = "Wed";
    } else if (userweekday==5){
        dayOfTheWeek = "Thu";
    } else if (userweekday==6){
        dayOfTheWeek = "Fri";
    } else if (userweekday==7){
        dayOfTheWeek = "Sat";
    } else if (userweekday==1){
        dayOfTheWeek = "Sun";
    }

        String birthdate = month2 + "/" + day2 + "/" + year2 + "," + dayOfTheWeek;



        System.out.println ("Your birthday was" + " " + birthdate);

 }

最佳答案

您做事的顺序错误。正如问题中的代码所示,发生了以下情况:


您正在将month2day2year2初始化为0。
您正在使用值为0的变量创建GregorianCalendar对象。这使您获得公元前2年12月31日(星期三)(没有年份0,因此您将公元1年1月1日设置为0,即12月31日)。公元前2年-令人困惑,但对于GregorianCalendar而言,这是事实。然后,将一周中的某天带入userweekday(始终等于Calendar.WEDNESDAY)。
最后,在year2 = Integer.parseInt(useryear);等行中,您正在将用户输入分配给变量。这不会影响您已经创建的GregorianCalendar对象,也不会影响该GregorianCalendar的星期几。


因此,将始终打印星期三。

顺便说一句,GregorianCalendar类早已过时并且存在设计问题。除其他问题外,它通常需要冗长的代码,这些代码也容易出错。而且它以出乎意料的方式编号了几个月。相反,我建议您使用java.time(现代Java日期和时间API)中的LocalDate

LocalDate              // Represent a date-only value, without time-of-day, without time zone.
.of( y , m , d )       // Specify year-month-day, 1-12 for January-December.
.getDayOfWeek()        // Extract a `DayOfWeek` enum object representing one of seven days of the week.
.getDisplayName(       // Automatically localize the text of the name of the day of the week.
    TextStyle.SHORT ,  // Specify how long or abbreviated.
    Locale.US          // Locale determines the human language and cultural norms used in localizing.
)


链接:Oracle tutorial: Date Time解释如何使用java.time

关于java - 公历:从自己的生日输入中获取用户的(day_of_the_week),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52661046/

10-11 01:24