我要做的是写一个程序,你输入两位数字 然后他们被转换成印刷的单词。问题是这个程序 输入两位数字后挂起,我不明白为什么。任何和所有的帮助表示赞赏。 我是初学者,所有我可以用来解决这个问题基本上是if和switch。再次感谢。
#include <stdio.h>
int main (void)
{
int firstNum, secondNum;
printf("Enter a two digit number: ");
scanf("%d%d", &firstNum,&secondNum);
if (firstNum == 1 && secondNum == 0){
printf("You entered the number ten\n");}
if (firstNum == 1 && secondNum == 1){
printf("You entered the number eleven\n");}
if (firstNum == 1 && secondNum == 2){
printf("You entered the number twelve\n");}
if (firstNum == 1 && secondNum == 3){
printf("You entered the number thirteen\n");}
if (firstNum == 1 && secondNum == 4){
printf("You entered the number forteen\n");}
if (firstNum == 1 && secondNum == 5){
printf("You entered the number fifteen\n");}
if (firstNum == 1 && secondNum == 6){
printf("You entered the number sixteen\n");}
if (firstNum == 1 && secondNum == 7){
printf("You entered the number seventeen\n");}
if (firstNum == 1 && secondNum == 8){
printf("You entered the number eighteen\n");}
if (firstNum == 1 && secondNum == 9){
printf("You entered the number nineteen\n");}
switch(firstNum){
case 2: printf("You entered the number twenty-");break;
case 3: printf("You entered the number thirty-");break;
case 4: printf("You entered the number forty-");break;
case 5: printf("You entered the number fifty-");break;
case 6: printf("You entered the number sixty-");break;
case 7: printf("You entered the number seventy-");break;
case 8: printf("You entered the number eighty-");break;
case 9: printf("You entered the number ninty-");break;
}
switch (secondNum){
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;
}
return 0;
}
答案 0 :(得分:3)
您的程序“挂起”,因为它正在等待第二个号码。
而不是 4 2 ENTER ,输入 4 2 ˚F 0 0 输入
您需要验证scanf()
if (scanf("%d%d", &firstNum, &secondNum) != 2) {
fprintf(stderr, "Oops, the scanf didn't read 2 numbers.\n");
} else {
/* continue with program */
/* you might as well see what scanf got from the input */
printf("scanf got the values %d and %d.\n", firstNum, secondNum);
}
答案 1 :(得分:1)
一个非常简单的代码(特别适合初学者)。
#include <stdio.h>
#include <conio.h>
int main()
{
int num,n,r;
printf("Enter a two-digit number: ");
scanf("%d",&num);
n = num/10;
r = num%10;
switch(n)
{
case 1: switch(r)
{
case 0: printf("Ten");
break;
case 1: printf("Eleven");
break;
case 2: printf("Twelve");
break;
case 3: printf("Thirteen");
break;
case 4: printf("Fourteen");
break;
case 5: printf("Fifteen");
break;
case 6: printf("Sixteen");
break;
case 7: printf("Seventeen");
break;
case 8: printf("Eighteen");
break;
case 9: printf("Nineteen");
}
break;
case 2: printf("Twenty-");
break;
case 3: printf("Thirty-");
break;
case 4: printf("Fourty-");
break;
case 5: printf("Fifty-");
break;
case 6: printf("Sixty-");
break;
case 7: printf("Seventy-");
break;
case 8: printf("Eighty-");
break;
case 9: printf("Ninety-");
break;
}
if(n != 1)
{
switch(r)
{
case 1: printf("one");
break;
case 2: printf("two");
break;
case 3: printf("three");
break;
case 4: printf("four");
break;
case 5: printf("five");
break;
case 6: printf("six");
break;
case 7: printf("seven");
break;
case 8: printf("eight");
break;
case 9: printf("nine");
break;
default: ;
}
}
getch();
}
答案 2 :(得分:0)
您的程序从控制台读取数据,控制台处于“熟”模式。在此模式下,控制台收集输入并允许用户编辑它。按返回或 Enter 后,数据将发送到程序。
您想要的模式是“原始”模式。有多种方法可以进入原始模式,但这取决于您的操作系统。
快速解决方法是输入两位数并按返回