1.if语句结构
int time = 25 ;
if ( time >= 0 && time <= 12 ) {
System . out. println ( "上午好" ) ;
} else if ( 13 <= time && time<= 18 ) {
System . out. println ( "下午好" ) ;
} else if ( 19 <= time && time<= 24 ) {
System . out. println ( "晚上好" ) ;
} else {
System . out. println ( "输入错误" ) ;
}
2.switch语句结构
@Test
public void test ( ) {
doWhat ( 8 ) ;
}
public void doWhat ( int day) {
if ( day> 7 || day< 1 ) {
System . out. println ( "您输入错误,请重新输入" ) ;
return ;
}
switch ( day) {
case 1 :
System . out. println ( "星期一" ) ;
break ;
case 2 :
System . out. println ( "星期二" ) ;
break ;
case 3 :
System . out. println ( "星期三" ) ;
break ;
case 4 :
System . out. println ( "星期四" ) ;
break ;
case 5 :
System . out. println ( "星期五" ) ;
break ;
case 6 :
System . out. println ( "星期六" ) ;
break ;
case 7 :
System . out. println ( "星期天" ) ;
break ;
}
3.星期枚举类
public enum Week {
星期一, 星期二, 星期三, 星期四, 星期五, 星期六, 星期天
}
public class Student {
public Week day;
public void doWhat ( Week day) {
switch ( day) {
case 星期一:
System . out. println ( "星期一" ) ;
break ;
case 星期二:
System . out. println ( "星期二" ) ;
break ;
case 星期三:
System . out. println ( "星期三" ) ;
break ;
case 星期四:
System . out. println ( "星期四" ) ;
break ;
case 星期五:
System . out. println ( "星期五" ) ;
break ;
case 星期六:
System . out. println ( "星期六" ) ;
break ;
case 星期天:
System . out. println ( "星期天" ) ;
break ;
}
}
@Test
public void test ( ) {
doWhat ( Week . 星期一) ;
}
}
public class Student {
public enum Week {
星期一, 星期二, 星期三, 星期四, 星期五, 星期六, 星期天
}
public void doWhat ( Week day) {
switch ( day) {
case 星期一:
System . out. println ( "星期一" ) ;
break ;
case 星期二:
System . out. println ( "星期二" ) ;
break ;
case 星期三:
System . out. println ( "星期三" ) ;
break ;
case 星期四:
System . out. println ( "星期四" ) ;
break ;
case 星期五:
System . out. println ( "星期五" ) ;
break ;
case 星期六:
System . out. println ( "星期六" ) ;
break ;
case 星期天:
System . out. println ( "星期天" ) ;
break ;
}
}
@Test
public void test ( ) {
doWhat ( Week . 星期一) ;
}