Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 公历:获取用户';从他们自己的生日输入_Java_Gregorian Calendar - Fatal编程技术网

Java 公历:获取用户';从他们自己的生日输入

Java 公历:获取用户';从他们自己的生日输入,java,gregorian-calendar,Java,Gregorian Calendar,我是一个非常业余的编码员,似乎无法从用户输入的整数中获取一周中的某一天(如星期日、星期一等)的问题。我只是想给一个用户一条消息,其中列出了(mm/dd/yyyy,dayOfWeek),但是我没有改变一周中的某一天,而是一直把星期三作为一周中的某一天来回答,不管我在提示框中输入了什么。我只是需要一个新的方向。我的代码有错误吗?或者我没有看到的途径?任何帮助都将不胜感激 public static void main(String [] args) { Scanner use

我是一个非常业余的编码员,似乎无法从用户输入的整数中获取一周中的某一天(如星期日、星期一等)的问题。我只是想给一个用户一条消息,其中列出了(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);

 }      

你在按错误的顺序做事。问题中的代码如下所示:

  • 您正在将
    month2
    day2
    year2
    初始化为0
  • 您正在从那些值为0的变量创建
    GregorianCalendar
    对象。这给了你星期三,公元前2年12月31日(没有0年,所以你设置为公元前1年1月0日,等于公元前2年12月31日-令人困惑,但对于
    Gregorianalendar
    来说,这是事实)。然后将一周中的一天放入
    userweekday
    (始终等于
    日历。星期三
  • 最后在
    year2=Integer.parseInt(useryear)行中,等等,将用户输入分配给变量。这不会影响您已经创建的
    GregorianCalendar
    对象,也不会影响从该
    GregorianCalendar
    获取的星期几
  • 因此,星期三将一直打印

    另外,
    gregoriacalendar
    类早已过时,存在设计问题。在其他问题中,它通常需要冗长的代码,这也可能容易出错。它以一种意想不到的方式计算月数。相反,我建议您使用java.time中的
    LocalDate
    ,这是现代java日期和时间API

    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. 
    )
    

    链接:解释如何使用
    java.time

    userbirth=new GregorianCalendar(year2,month2,day2)
    。。。您正在使用0,0,0初始化用户生日,这可能是一个星期三。不要使用GregoriaCalendar。它是一个设计得非常好的类,已经被java.time包的类所取代。使用它(LocalDate可能是您所需要的全部)。感谢您的前两条建议,我更改了代码(int year=userbirth.get(Calendar.year);)等等,但我认为我不理解第三条建议。我将如何分配用户在扫描仪中输入的变量?@AnfanioluwaLawal如果我正确理解您的问题:您将用户输入分配给变量是正确的。在创建
    gregorianalendar
    对象之前,只需执行此操作(以及以下验证)。或者更好,在创建
    LocalDate
    对象之前(如我的答案底部所示,是Basil Bourque编辑的类型)。