C入门【五】(一)

内容核心 巩固练习C语言入门语法练习

1.编写一个程序,可以一直接收键盘字符

详细/*
如果是小写字符就输出对应的大写字符,
如果接收的是大写字符,就输出对应的小写字符,
如果是数字不输出。
*/
个人看法:这道题我在练习时有两种思路 1.将输入的字符先转化为int型 然后根据 ASCA|| 码表对照分类
2.直接对输入字符进行比对(这种方法更加简单也更加符合我们思考习惯)
话不多说上代码

int main(){
	char c;
	scanf("%c", &c);
	if ((c >= 'A') && (c <= 'Z')){
		c = c + 32;
		printf("%c", c);
	}else 
	if ((c>='a') && (c <='z')){
		c = c - 32;
		printf("%c", c);
	}
	else{
		printf("%c", c);
	}
	system("pause");
	return 0;
}

一看代码一目了然

2.猜数字游戏

//猜数字游戏顾名思义就是 输入一个数字与目标数字进行比对符合情况输出不符合情况在有提示的情况下继续直到结束
个人看法:1/在执行的过程中核心的是方法是如何进行比对
2.如何产生随机数 random方法 int random = rand() % 100 + 1; 但是在这里我们要理解随机数产生时如果不清理缓存池(暂时这样称呼) 始终会产生同样个值,为了不让小游戏失去趣味我们需要在每次开始时重新生成一个新的随机数 srand((unsigned)time(NULL)); 这个方法就是保证每次生成新随机数

#define _CRT_SECURE_NO_WARNINGS 1
#include "stdio.h"
#include "stdlib.h"
#include "time.h"
#define random(x) (rand()%x)   //利用宏定义产生随机数
/*
	需求分析 完成猜数字游戏
	1.完成业务框架{
					进入游戏
					退出游戏
				}
	2.业务功能 {
				比对数字(二分法)
*/
int Menu(){
	printf("********************\n");
	printf("*****1.进入游戏*****\n");
	printf("*****0.退出游戏*****\n");
	printf("********************\n");
	return 0;
}

int Game(){
	int input;
	int random = rand() % 100 + 1;
	while (1){
		printf("请输入的数字:");
		scanf("%d", &input);
		if (input > random){
			printf("大了\n");
		}
		if (input < random){
			printf("小了\n");
		}
		if (input == random){
			printf("对了\n");
			break;
			system("pause");
		}
	}
}
int main(){
	int input = 0;
	srand((unsigned)time(NULL));
	do{
		Menu();
		printf("请选择");
		scanf("%d", &input);
		switch (input){
		case 1:
			Game();
			break;
		case 0:
			break;
		default:
			printf("重新输入\n");
			break;
		}

	} while (input);
	system("pause");
	return 0;
}

这为第一部分内容

猜你喜欢

转载自blog.csdn.net/qq_36390039/article/details/84777382
今日推荐