gpt4 book ai didi

java数组变量初始化

转载 作者:行者123 更新时间:2023-11-30 02:58:04 25 4
gpt4 key购买 nike

大家好,我只是想问一下该怎么办..我想创建一个程序,在其中可以显示所有输入的日期而无需分隔符“/”,所以我使用 split 方法来做到这一点。更清楚地说这是我想做的:

Input
Enter Date:10/11/1994
Enter Date:11/10/2008
Enter Date:12/12/2010
Enter Date:08/12/1999
Enter Date:09/10/2005

Output:
10 11 1994
11 10 2008
12 12 2010
08 12 1999
09 10 2005

问题是我有一个错误在System.out.println(comp[ctr1]);它说我必须初始化 comp变量,实际上我不知道我将使用什么初始化。我尝试使用 String[] comp=new String[date]String[] comp=new String[5]但它仍然是一个错误..提前致谢..

String[] date=new String[5];
String[] comp;
int mm, dd, yyyy;
for(int ctr=0;ctr<date.length;ctr++){
System.out.print("Enter Date: ");
date[ctr]=input.nextLine();
comp=date[ctr].split("/");
mm=Integer.parseInt(comp[0]);
dd=Integer.parseInt(comp[1]);
yyyy=Integer.parseInt(comp[2]);
}
for(int ctr1=0;ctr1<date.length;ctr1++){
System.out.println(comp[ctr1]);
}

最佳答案

为什么要重新发明轮子?家庭作业?为什么不使用java工具来实现你的目标呢?

DateFormat input = new SimpleDateFormat("MM/dd/yyyy"); 
DateFormat output = new SimpleDateFormat("MM dd yyyy");

Date d = input.parse("10/11/1994");
System.out.println(output.format(d));

输出:

10 11 1994

关于java数组变量初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36692709/

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