作业:
- 分支语句:if-else,break,switch,return;
- 循环语句:while,do-while,for,continue;
- 例外处理语句:try-catch-finally,throw;
- 数组-二维数组,Java中列表的使用
- 列表使用 区分set和List的区别
输出星期一到星期日的英文单词,几种不同的实现。
技能学习-如果能和实际情况联系起来,就可以做到学以致用,使用一次后,才会有第二次使用,才会有举一反三。
//if-else
public void printWeek(int day) {
int i = 0;
i = day % 7;
if (i < 6) {
System.out.println("今天是:" + week[i] + ",是个工作日");
} else {
System.out.println("今天是:" + week[i] + ",是个休息日");
}
}
//switch-case
public void printForWeek(int day) {
switch (day) {
case 1:
System.out.println("printForWeek今天是:" + week[0] + ",是个工作日");
break;
case 2:
System.out.println("printForWeek今天是:" + week[1] + ",是个工作日");
break;
case 3:
System.out.println("printForWeek今天是:" + week[2] + ",是个工作日");
break;
case 4:
System.out.println("printForWeek今天是:" + week[3] + ",是个工作日");
break;
case 5:
System.out.println("printForWeek今天是:" + week[4] + ",是个工作日");
break;
case 6:
System.out.println("printForWeek今天是:" + week[5] + ",是个休息日");
break;
case 7:
System.out.println("printForWeek今天是:" + week[6] + ",是个休息日");
break;
default:
System.out.println("printForWeek请输入1-7的数字");
}
}
//while
public void printForWeekWhile() {
int i = 0;
while (i < week.length) {
System.out.println("printForWeekWhile今天是:" + week[i] + "");
i++;
}
}
//do-while
public void printForWeekDo() {
int i = week.length;
do {
System.out.println("printForWeekDo今天是:" + week[i - 1] + "");
i--;
}
while (i > 0);
}
//打印二维数组,第一个纬度是数组,第二个纬度是定义一种数据结构
//网上的例子,比如 {zhangsan,男,23岁},{李四,男,24岁}
public void printFowWeekS() {
String weeks[][] = {{"Mon","1","星期一"},
{"Tue","2","星期二"},
{"Wed","3","星期三"},
{"Thu","4","星期四"},
{"Fri","5","星期五"},
{"Sat","6","星期六"},
{"Sun","7","星期日"}};
for (int i = 0; i < weeks.length; i++) {
System.out.println("printFowWeekS数组:" + i);
for (int j = 0; j < weeks[i].length; j++) {
System.out.print(" " + weeks[i][j] + " ");
}
System.out.println();
}
}
//根据日期打印星期几
public int dayForWeek(String pTime) throws Exception {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.setTime(format.parse(pTime));
int dayForWeek = 0;
if (c.get(Calendar.DAY_OF_WEEK) == 1) {
dayForWeek = 7;
} else {
dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
}
return dayForWeek;
}
//验证
public static void main(String[] args) {
int week = 0;
Logic logic = new Logic();
logic.printWeek(2);
logic.printWeek(3);
logic.printWeek(6);
try {
week = logic.dayForWeek("2016-01-31");
System.out.println("今天星期" + week);
week = logic.dayForWeek("2016-02-01");
System.out.println("今天星期" + week);
week = logic.dayForWeek("eee");
}
catch (Exception e) {
System.out.println("日期格式不对");
e.printStackTrace();
}finally{
System.out.println("始终都会执行的语句");
}
logic.printForWeek(6);
logic.printForWeek(8);
logic.printForWeekWhile();
logic.printForWeekDo();
logic.printFowWeekS();
}
结果
今天是:Wed,是个工作日
今天是:Thu,是个工作日
今天是:Sun,是个休息日
今天星期7
今天星期1
日期格式不对
java.text.ParseException: Unparseable date: "eee"
at java.text.DateFormat.parse(Unknown Source)
at pers.qingqian.study.basic.Logic.dayForWeek(Logic.java:82)
at pers.qingqian.study.basic.Logic.main(Logic.java:47)
始终都会执行的语句
printForWeek今天是:Sat,是个休息日
printForWeek请输入1-7的数字
printForWeekWhile今天是:Mon
printForWeekWhile今天是:Tue
printForWeekWhile今天是:Wed
printForWeekWhile今天是:Thu
printForWeekWhile今天是:Fri
printForWeekWhile今天是:Sat
printForWeekWhile今天是:Sun
printForWeekDo今天是:Sun
printForWeekDo今天是:Sat
printForWeekDo今天是:Fri
printForWeekDo今天是:Thu
printForWeekDo今天是:Wed
printForWeekDo今天是:Tue
printForWeekDo今天是:Mon
printFowWeekS数组:0
Mon 1 星期一
printFowWeekS数组:1
Tue 2 星期二
printFowWeekS数组:2
Wed 3 星期三
printFowWeekS数组:3
Thu 4 星期四
printFowWeekS数组:4
Fri 5 星期五
printFowWeekS数组:5
Sat 6 星期六
printFowWeekS数组:6
Sun 7 星期日