练习if-else语句
用if-else语句实现以下代码:
编写一个程序,输入日期和时间,输出在该时间段在做什么事情。
时间表如下:
Weekdays(Day1-5):
0:00--9:00:sleep
9:01--19:00:work
19:01--23:59:relax
Weekend(Day6-7):
0:00--11:00:sleep
11:01--23:59:have fun
#include <stdio.h>
int main()
{
int day;
int time;
printf("请输入今天的日期和时间:");
scanf_s("%d%d", &day, &time);
if (day > 1 && day < 5)
{
if (time <= 900)
printf("sleep");
if (time < 1900 && time>900)
printf("work");
else
printf("relax");
}
else
{
if (time <= 1100)
printf("sleep");
else
printf("have fun");
}
}
注:&&表示and,表示并列关系,取交集。
switch语句
#include <stdio.h>
int main()
{
int score;
printf("请输入你的成绩:");
scanf_s("%d", &score);
switch (score/10)
{
case 9:printf("A");
break;
case 8:printf("B");
break;
case 7:printf("C");
break;
case 6:printf("D");
break;
default:printf("You didn't pass!");
}
}