- import java.util.Calendar;
- /*
- 如何定义函数:1,既然函数是一个独立的功能,那么功能的运算结果应明确(明确返回值类型)
- 2,再明确定义该功能的过程中是否需要未知内容参与运算(明确参数列表:参数的类型、个数)
- */
- class Test{
- public static void main(String[] args){
- Calendar c=Calendar.getInstance();
- String month = getMonth(c);
- String weeks = getWeek(c);
- }
- /**
- * 定义一个函数可以得到myWeek
- */
- private static String getWeek(Calendar c) {
- String[] weeks={"星期一","星期二","星期二","星期三","星期四","星期五"
- ,"星期六","星期日"};
- int index2=c.get(Calendar.DAY_OF_WEEK);
- String myWeek=weeks[index2];
-
- return myWeek;
- }
- /**
- * 定义函数得到myMonth
- */
- private static String getMonth(Calendar c) {
- String[] month={ "一月","二月","三月","四月","五月",
- "六月","七月","八月","九月","十月","十一月","十二月"};
- int index1=c.get(Calendar.MONTH);
- String myMonth=month[index1];
-
- return myMonth;
- }
- }
复制代码 |