使用函数和if-else语句在C中打印出一个2位数的拼写(初学者)

r7xajy2e  于 2023-11-16  发布在  其他
关注(0)|答案(2)|浏览(104)

嗨,我不知道为什么我的代码没有显示出我预期的结果。练习是用C中的函数和if-else语句打印出一个2位数的拼写。当我运行代码时,它什么也没有返回。(这是初学者的练习,所以我只能想到这一点,所以很抱歉:(()你能检查一下我错在哪里吗?非常感谢!

#include <stdio.h>

// Consider the second number in that 2-digit number when the first number is 1
void ten(char b)
{
    switch (b) {
        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("fifthteen"); break;
        case(6): printf("sixteen"); break;
        case(7): printf("seventeen"); break;
        case(8): printf("eighteen"); break;
        case(9): printf("nineteen"); break;
    }
}

// Consider the second number in that 2-digit number when the first number is not 1
void morethanten(char b)
{
    switch (b) {
        case(0): break;
        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;
    }
}

int main()
{
    char a, b;
    printf("Enter your 2-digit number: ");
    scanf("%c%c", &a, &b);

    // Consider the first number in that 2-digit number
    switch (a) {
        case(0): printf("That is not a 2-digit number"); break;
        case(1): ten(b); break;
        case(2): printf("Twenty"); morethanten(b); break;
        case(3): printf("Thirty"); morethanten(b); break;
        case(4): printf("Fourty"); morethanten(b); break;
        case(5): printf("Fifthty"); morethanten(b); break;
        case(6): printf("Sixty"); morethanten(b); break;
        case(7): printf("Seventy"); morethanten(b); break;
        case(8): printf("Eighty"); morethanten(b); break;
        case(9): printf("Ninety"); morethanten(b); break;
    }
    return 0;
}

字符串

iq0todco

iq0todco1#

在case语句中,检查的是整数值,而不是数字的字符表示。

case(1): ten(b); break;

字符串
您对该特定声明的案例比较将是:

case('1'): ten(b); break;


这里有一个快速测试后,纠正情况比较,使测试对一个字符。

craig@Vera:~/C_Programs/Console/Spelling/bin/Release$ ./Spelling 
Enter your 2-digit number: 12
twelve


给予一个尝试。另外,你可能想参考一些教程文献,以更好地理解在开关块内的测试。

2wnc66cl

2wnc66cl2#

switch (b) {
        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; // Little error for name
        case 6: printf("sixteen"); break;
        case 7: printf("seventeen"); break;
        case 8: printf("eighteen"); break;
        case 9: printf("nineteen"); break;
    }

switch (a) {
          case '0': printf("That is not a 2-digit number"); break;
          case '1': ten(b); break;
          case '2': printf("Twenty "); morethanten(b); break; 
          case '3': printf("Thirty "); morethanten(b); break; 
          case '4': printf("Forty "); morethanten(b); break;
          case '5': printf("Fifty "); morethanten(b); break; 
          case '6': printf("Sixty "); morethanten(b); break; 
          case '7': printf("Seventy "); morethanten(b); break;
          case '8': printf("Eighty "); morethanten(b); break; 
          case '9': printf("Ninety "); morethanten(b); break; 
    }

字符串

相关问题