练习打印1000到2000年之间的闰年
#include<stdio.h>
int main()
{
int year = 0;
for (year = 1000; year <= 2000; year++)//判断是否为闰年的两个条件1.能被4整除并且不能被100整除 2.能被400整除
{
if (year % 4 == 0 && year % 100 != 0)//年份磨4等于0说明能被4整除,并且磨100不等于0说明不能被100整除
{
printf("%d ", year);
}
else if(year % 400 == 0)//这里表示能被400整除
{
printf("%d ", year);
}
}
}
99乘法口诀
int i = 0;
for (i = 1; i <= 9; i++)//i表示行数,i小于等于9说明打印9行
{
int j = 1;
for (j = 1; j <= i;j++)//j表示列数,
{
printf(" % d * %d = %2d", i, j, i * j);
}
printf("\n");
}
}
goto语句
#include<stdio.h>
int main()
{
a:
printf("哈喽\n");
goto a;
return 0;
}
简单关机程序(使用goto)
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>//shutdown的头文件
#include<string.h>//strcmp的头文件
int main()
{
char i[10] = { 0 };//i用来存放输入的数,表述能存放10个字符
system("shutdown -s -t 60");//system是执行系统命令的,shutdown -s -t 60是表示在60秒后关机
again:
printf("你的电脑在一分钟后关机,输入我是猪取消\n请输入>:");
scanf("%s", &i);
if (strcmp(i, "我是猪") == 0)//比较两个字符串的函数—strcmp
{
system("shutdown -a");//shutdown -a取消关机
}
else//输入了其他数就跳到上面让他重新输入
{
goto again;
}
return 0;
}
简单关机程序(不用goto)
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>//shutdown的头文件
#include<string.h>//strcmp的头文件
int main()
{
char i[10] = { 0 };//i用来存放输入的数,表述能存放10个字符
system("shutdown -s -t 60");//system是执行系统命令的,shutdown -s -t 60是表示在60秒后关机
while (1)
{
printf("你的电脑在一分钟后关机,输入我是猪取消\n请输入>:");
scanf("%s", &i);
if (strcmp(i, "我是猪") == 0)//比较两个字符串的函数—strcmp
{
system("shutdown -a");//取消关机
break;
}
}
return 0;
}
猜数字游戏
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void menu()
{
printf("****************************************************\n");
printf("****** 1.play 0.exit ******\n");
printf("****************************************************\n");
}
void game()
{
int ret = 0;
int guess = 0;//接收猜的数字
ret = rand()%100+1;//rand是生成0-32767之间的随机数 %100就是0-100的数
while (1)
{
printf("请猜数字>");
scanf("%d", &guess);
if (guess > ret)
{
printf("猜大了\n");
}
else if (guess < ret)
{
printf("猜小了\n");
}
else
{
printf("恭喜猜对了\n");
break;
}
}
}
int main()
{
int input = 0; //时间戳是当前计算机的时间减去计算机的起始时间(1970.1.1.0:0:0)
srand((unsigned int)time(NULL));//time 拿时间戳来设置随机起点,srand表示设置一个随机起点,使用rand之前要先调用srand,NULL是空指针,因为time后面要指针
do //time强制转换成unsigned int型,因为srand要unsigned int 型
{
menu();//简单的菜单函数
printf("请选择>");
scanf("%d", &input);
switch (input)
{
case 1:
game();//玩游戏
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误\n");
break;
}
} while (input);//如果input是0则为假,退出,如果玩完一局input为1为真所以继续循环就表示可以继续玩,或者选择错误了也继续循环,重新选择
return 0;
}