Program logic control (1)-sequence structure and branch structure (if statement and switch statement) and the code implementation of their related exercises (determine whether it is a leap year, determine the day of the week, etc.)
Determine odd and even numbers and positive and negative numbers
publicclassTestDemo1{
publicstaticvoidmain(String[] args){
int num =10;if(num %2==1){
System.out.println("num是奇数");}else{
System.out.println("num是偶数");}if(num >0){
System.out.println("num 是正数");}elseif(num <0){
System.out.println("num 是负数");}else{
System.out.println("num 是 0");}}
Judging Leap Year
publicstaticvoidmain(String[] args){
int year =2000;if(year %100==0){
// 判定世纪闰年if(year %400==0){
System.out.println("是闰年");}else{
System.out.println("不是闰年");}}else{
// 普通闰年if(year %4==0){
System.out.println("是闰年");}else{
System.out.println("不是闰年");}}}
2.switch statement
Note :
According to the different values in switch, the corresponding case statement will be executed. When a break is encountered, the case statement will end;
If the value in switch does not match the case, the statement in default will be executed;
We recommend that a switch statement should be accompanied by default;
//语法switch(整数|枚举|字符|字符串){
case 内容1:{
内容满足时执行语句;[break;]}case 内容2:{
内容满足时执行语句;[break;]}...default:{
内容都不满足时执行语句;[break;]}}
publicstaticvoidmain(String[] args){
int day =1;switch(day){
case1:
System.out.println("星期一");break;case2:
System.out.println("星期二");break;case3:
System.out.println("星期三");break;case4:
System.out.println("星期四");break;case5:
System.out.println("星期五");break;case6:
System.out.println("星期六");break;case7:
System.out.println("星期日");break;default:
System.out.println("输入有误");break;}