一、實現(xiàn)功能:
輸入表示年齡的數(shù)字,將其轉化為英文
二、相關知識:
1、數(shù)組
2、條件語句
三、代碼實現(xiàn)
#include <stdio.h>
//輸入年齡轉化為對應英文
//數(shù)組相關知識
int main(){
int age = 0;
//保存?zhèn)€位數(shù)
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"};
//保存十位數(shù)
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){
//獲得個位數(shù)
int index = age % 10;
name = temp[index];
} else{
//獲取個位數(shù)
int g = age % 10;
//獲取十位數(shù)
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、條件語句中不需要執(zhí)行完畢直接結束可在該條件執(zhí)行完畢后中止程序,直接 return 0
3、各位與十位數(shù)分開存放在不同數(shù)組調用的思想
4、取整/ 與 取余%
取整決定十位數(shù)
取余求各位
數(shù)組相關知識在后面學習