循环中数组的交换值

循环中数组的交换值

我试图建立一个初学者的日历练习项目,我想要的只是一月的最后一天在星期日结束,然后从星期一开始下个月。这是我的代码;

 public static void main(String[] args)
    {

        String[] months= {"January","Febuary","March","April","May","June"
                   ,"July","August","september","October","November"
                   ,"December"
                 };
        String[] weekdays= {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};

        int x;

        for(int k=0;k<12;k++)
          {
            if(months[k]=="April" ||months[k]=="june" ||months[k]=="September" ||months[k]=="November")
                {x=30;}
            else if(months[k]=="Febuary")
                {x=28;}
            else {x=31;}

            System.out.print(months[k]+"\n");


           for(int i=0;i<weekdays.length;i++)
             {
              System.out.print("\t"+weekdays[i]);
             }System.out.println();

           for(int m=1;m<=x;m++) {

               if(((m-1) %7) == 0)  //line break after 7 characters
               {
                   System.out.println();
               }
               System.out.print("\t"+m);
           }
           System.out.println();
    }
  }


java - 循环中数组的交换值-LMLPHP

最佳答案

我认为您使用的方法不正确。使用dated与使用Locale有关,因为所有这些名称(例如月份名称,工作日名称)都以不同的格式存在于JVM中。您应该使用它:

public static void printCalendar(LocalDate date, Locale locale) {
    DateFormatSymbols symbols = new DateFormatSymbols(locale);
    WeekFields weekFields = WeekFields.of(locale);

    printMonthName(symbols, date);
    printWeekdayNames(symbols, weekFields);
    printWeekdays(weekFields, date);
}

private static void printMonthName(DateFormatSymbols symbols, LocalDate date) {
    System.out.println(symbols.getMonths()[date.getMonthValue() - 1]);
}

private static void printWeekdayNames(DateFormatSymbols symbols, WeekFields weekFields) {
    String[] weekdays = symbols.getShortWeekdays();
    DayOfWeek firstDayOfWeek = weekFields.getFirstDayOfWeek();
    int offs = firstDayOfWeek == DayOfWeek.SUNDAY ? 1 : firstDayOfWeek.ordinal() + 2;

    for (int i = 0; i < 7; i++)
        System.out.print('\t' + (offs + i >= weekdays.length ? weekdays[(offs + i) % 7] : weekdays[offs + i]));

    System.out.println();
}

private static void printWeekdays(WeekFields weekFields, LocalDate date) {
    LocalDate cur = date.withDayOfMonth(1).with(weekFields.dayOfWeek(), 1);
    boolean stop = false;

    do {
        if (cur.getMonthValue() == date.getMonthValue())
            System.out.format("\t%2d", cur.getDayOfMonth());
        else
            System.out.format("\t  ");

        cur = cur.plusDays(1);

        if (cur.getDayOfWeek() == weekFields.getFirstDayOfWeek()) {
            System.out.println();
            stop = cur.getMonthValue() != date.getMonthValue();
        }
    } while (!stop);
}


测试1:

printCalendar(LocalDate.of(2018, Month.DECEMBER, 17), Locale.US);

December
    Sun Mon Tue Wed Thu Fri Sat
                             1
     2   3   4   5   6   7   8
     9  10  11  12  13  14  15
    16  17  18  19  20  21  22
    23  24  25  26  27  28  29
    30  31


测试2:

printCalendar(LocalDate.of(2018, Month.DECEMBER, 17), Locale.ITALIAN);

dicembre
    lun mar mer gio ven sab dom
                         1   2
     3   4   5   6   7   8   9
    10  11  12  13  14  15  16
    17  18  19  20  21  22  23
    24  25  26  27  28  29  30
    31

关于java - 循环中数组的交换值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53808592/

10-09 13:11