黑马程序员技术交流社区
标题:
数组角标越界,崩溃啊,找不到原因????
[打印本页]
作者:
彭波
时间:
2013-3-17 21:54
标题:
数组角标越界,崩溃啊,找不到原因????
本帖最后由 彭波 于 2013-3-18 08:45 编辑
import java.util.*;
import java.text.*;
class CalendarDemo
{
public static void main(String[] args)
{
Calendar c = Calendar.getInstance();
String[] mons = {"一月", "二月", "三月", "四月",
"五月", "六月", "七月", "八月",
"九月", "十月", "十一月", "十二月"};
String[] weeks = {
"", "星期日", "星期一", "星期二", "星期三",
"星期四", "星期五", "星期六"
};
int index = c.get(Calendar.MONTH);
int index1 = c.get(Calendar.DAY_OF_MONTH);
sop(c.get(Calendar.YEAR)+"年");
//sop((c.get(Calendar.MONTH)+1)+"月");
sop(mons[index]);
sop(c.get(Calendar.DAY_OF_MONTH)+"日");
//sop("星期"+c.get(Calendar.DAY_OF_WEEK));
sop(weeks[index1]);//
这个地方角标越界,和毕老师的代码一样啊??难道我晚上眼镜花了
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
毕老师所谓查表法输出日期,但我的代码报
角标越界错误,求大神????
作者:
刘国涛
时间:
2013-3-17 22:07
int index1 = c.get(Calendar.DAY_OF_MONTH);//MONTH改为WEEK就OK了
作者:
赵晓东
时间:
2013-3-17 22:09
/*Calendar 日历*/
import java.util.*;
class CalendarDemo
{
public static void main(String[] args)
{
Calendar c = Calendar.getInstance();
// c.set(2013,8,9);//设定时间值
c.add(Calendar.YEAR,2);//制定字段,加减时间量
printCalendar(c);
}
public static void printCalendar(Calendar c)
{
String[] month = {"一月 ","二月 ","三月 ","四月 ",
"五月 ","六月 ","七月 ","八月 ",
"九月 ","十月 ","十一月 ","十二月 "};
String[] week = {"","星期日","星期一","星期二",
"星期三","星期四","星期五","星期六",};
int index_month = c.get(Calendar.MONTH);
int index_week = c.get(Calendar.DAY_OF_WEEK);//你这里是DAY_OF_MONTH(一个月的第几天)当然越界了,改成DAY_OF_WEEK即可
sop(c.get(Calendar.YEAR)+"年");
//sop((c.get(Calendar.MONTH)+1)+"月");
sop(month[index_month]);
sop(c.get(Calendar.DAY_OF_MONTH)+"日");
//sop("星期"+c.get(Calendar.DAY_OF_WEEK));
sop(week[index_week]);
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
复制代码
作者:
赵晓东
时间:
2013-3-17 22:17
上面那个是我写的代码,一着急就给粘上来了
int index1 = c.get(Calendar.DAY_OF_MONTH); //你这里是DAY_OF_MONTH(一个月的第几天)当然越界了,改成DAY_OF_WEEK即可
作者:
HM李超
时间:
2013-3-17 23:55
int index1 = c.get(Calendar.DAY_OF_MONTH);这是嘛呢???int index1 = c.get(Calendar.DAY_OF_WEEK);正解!
源代码楼上给出来了,我就不贴了
作者:
彭波
时间:
2013-3-18 08:45
擦,我看错了,谢谢各位大侠
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2