写一个方法实现分时问侯, 如是8点至12点,返回"上午好", 12点至14点,返回"中午好", 14点至18点,返回"下午好", 其它时间返回"晚上好"

public class Test1{
public String sayHello(int hour) {
String msg;
if(hour >=8 && hour < 12) {
msg = “上午好”;
System.out.println(msg);}
else if(hour>=12 && hour <14){
msg = “中午好”;System.out.println(msg);}
else if(hour>=14 && hour <18) {
msg = “下午好”;System.out.println(msg);}
else {
msg = “晚上好”;
System.out.println(msg); }
return msg;

}
public static void main(String[] args) {
Test1 test1=new Test1();
test1.sayHello(12);
}
}

猜你喜欢

转载自blog.csdn.net/weixin_42428664/article/details/81808939
今日推荐