gpt4 book ai didi

java - 如何使用java中的日历类一次打印两个不同日期的星期几

转载 作者:行者123 更新时间:2023-11-30 02:40:34 24 4
gpt4 key购买 nike

我想从用户那里获取每行的年份和月份,并且我必须打印相应年份和月份的星期几,所有输入的日期均为 28,我为第一个输入和第二个输入打印了 day_of_week输入jvm抛出异常有人可以帮助我吗?

例如输入是:

  • 1999-5
  • 2000-7

控制台应该能够显示:

  • 1999 年 6 月 28 日
  • 2000 年 8 月 28 日。

月份从 0 到 11(0-11)

import java.util.Calendar;
public class Calendar1 {
public static void main(String[] args) {
int day = 0;
String[] input = new String[2];
int[] year = new int[1];
int[] month = new int[1];
String[] split = new String[2];
Calendar cal = Calendar.getInstance();
System.out.println("Enter The Year And Month(YYYY-M): ");
Scanner s = new Scanner(System.in);
for(int i=0;i<2;i++) {
input[i] = s.nextLine();
}
for(int i=0;i<2;i++) {
split = input[i].split("-");
year[i] = Integer.parseInt(split[0]);
month[i] = Integer.parseInt(split[1]);
cal.set(year[i],month[i],28);
day[i] = cal.get(Calendar.DAY_OF_WEEK);
System.out.println(day[i]);
}
}
}

最佳答案

你的数组边界似乎不合适。可能会改变你的它们

int day = 0;
int[] year = new int[1]; // array containing only one element
int[] month = new int[1];

int[] day = new int[N];
int[] year = new int[N];
int[] month = new int[N]; // where N should be the number of lines of your input (2 for the above use case)

关于java - 如何使用java中的日历类一次打印两个不同日期的星期几,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41867216/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com