我是 C 语言的新手,我无法为学校做一个简单的练习。
我想做这样的事情:
Please insert a number: 12345
five
four
three
two
one
基本上,用户输入一个数字,然后程序在新的一行中写入从最后一个有效数字到最大数字的数字。
这与开关功能有关,只是基本的编程技巧。
我有这个:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num; printf("Please insert a number: "); scanf("%d", &num);
switch(num){
case 1:printf("One\n");break;
case 2:printf("Two\n");break;
case 3:printf("Three\n");break;
case 4:printf("Four\n");break;
case 5:printf("Five\n");break;
case 6:printf("Six\n");break;
case 7:printf("Seven\n");break;
case 8:printf("Eight\n");break;
case 9:printf("Nine\n");break;
case 0:printf("Zero\n");break;
}
}
我使用 0 到 9 之间的数字它工作正常但是一个更大的数字它什么都不做。
我无法解决的第一个问题是,在数字中,获取数字位置。 我相信在我的代码中,中断并不是什么都不做......
对不起,如果我不能更好地解释我,但英语不是我的母语。
问候,
法沃拉斯
############################## 进行中的解决方案(如果数字 % 10 给出 0¬¬¬¬¬¬¬¬口#include <stdio.h>
#include <stdlib.h>
int main()
{
int num; printf("Please insert a number: "); scanf("%d", &num);
int i,digit;
for (i = 0; num%10!=0;i++){
digit = num % 10;
switch(num){
case 1:printf("One\n");break;
case 2:printf("Two\n");break;
case 3:printf("Three\n");break;
case 4:printf("Four\n");break;
case 5:printf("Five\n");break;
case 6:printf("Six\n");break;
case 7:printf("Seven\n");break;
case 8:printf("Eight\n");break;
case 9:printf("Nine\n");break;
case 0:printf("Zero\n");break;
}
num = num / 10;
}
}
最佳答案
数字 123
是 100 + 20 + 3
或 1*10^2 + 2*10^1 + 3*10^0
.
您需要从输入中分离出各个数字。
提示:使用/
和%
运算符
12345 % 10 ==> 5
12345 / 10 ==> 1234
1234 % 10 ==> 4
1234 / 10 ==> 123
123 ... ... ...
关于C 在新行中一个接一个地写数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5166540/