一、实现功能:
输入表示年龄的数字,将其转化为英文
二、相关知识:
1、数组
2、条件语句
三、代码实现
#include <stdio.h>
//输入年龄转化为对应英文
//数组相关知识
int main(){
int age = 0;
//保存个位数
char* gewei[] = {"","one","two","three","four","five","six","seven","eight","nine"};
//保存10—19
char* temp[] = {"ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
//保存十位数
char* shiwei[] = {"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninty"};
printf("请输入年龄:");
scanf("%d",&age);
char *name;//保存每次取出的字符串
if(age < 10){
name = gewei[age];
}else if(age >= 10 && age <= 19){
//获得个位数
int index = age % 10;
name = temp[index];
} else{
//获取个位数
int g = age % 10;
//获取十位数
int s = age / 10;
//取出对应的单词
char* sString = shiwei[s-2];
char* gString = gewei[g];
printf("your age is %s-%s\n",sString,gString);
//程序还未运行完毕提前结束
return 0;
}
printf("your age is %s",name);
return 0;
}
四、运行结果:
1、1到9岁
image.png
2、10到19岁
image.png
3、20到99岁
image.png
五、注意点:
1、变量类型不可前后矛盾需要仔细(int 与 char*)
2、条件语句中不需要执行完毕直接结束可在该条件执行完毕后中止程序,直接 return 0
3、各位与十位数分开存放在不同数组调用的思想
4、取整/ 与 取余%
取整决定十位数
取余求各位
数组相关知识在后面学习
网友评论