switch语句c语言的问题

issue with switch statement c language

这就是我正在处理的代码。一切都很好,只是我需要将表壳与计数器相匹配,这样我才能设法显示所需的输出“你在十岁/十几岁”。我尝试了很多不同的方法,但我做对了。但是,我认为问题在条件之内。有小费吗?注:我只是一个初学者,这学期刚开始学C编程!

#include <stdio.h>

int main()
{
    int YoB, CY, Age;
    unsigned int counter = 0;
    printf("Please enter your year of birth");
    scanf_s("%d", &YoB);
    printf("Please enter the current year");
    scanf_s("%d", &CY);
    Age = CY - YoB;
    printf("Entered year of birth %d\n", YoB);
    printf("Entered current year %d\n", CY);
    printf("You are %d years old\n \n", Age);
    if (Age > 18) {
        puts("You are an adult\n");
        if (Age < 18)
            puts("You are a minor\n");
    }
    if(Age<=100){
        while (++counter <= 10);
    }
    switch (counter) {
    case 0:
        puts("You are less than 10\n");
        break;
    case 1:
        puts("you are in your tens / teens\n");
        break;
    case 2:
        puts("You are in your twenties\n");
        break;
    case 3:
        puts("You are in your thirties\n");
        break;
    case 4:
        puts("You are in your fourties\n");
        break;
    case 5:
        puts("You are in your fifties\n");
        break;
    case 6:
        puts("You are in your sixties\n");
        break;
    case 7:
        puts("You are in your seventies\n");
        break;
    case 8:
        puts("You are in your eighties\n");
        break;
    case 9:
        puts("You are in your nineties\n");
        break;
    case 10:
        puts("You are a 100+!!\n");
        break;
    default:
        puts("invalid age!");
        break;
    }
    return 0;
}

问题的答案

你的“counter-setter-loop”中有一个简单的逻辑问题(我不知道该如何称呼它)。

if(Age<=100){
     while (++counter <= 10);
}

您正在将 counter 增加到 11 而不是 10。假设 counter 为 10,它会再次迭代,因为条件 <= 10 仍然为真。因此,您需要将此部分更改为:

if(Age<=100){
     while (++counter < 10);
}

代码审查

无论如何,当我看到你的代码时,我的脑海里浮现出一些代码改进的想法。我希望这对你没问题,如果不合适,你可以跳过这个。

  1. \n 添加到 printf(小 UI 改进)

在我看来,如果您创建一个小提示并为输入创建一个新行,它看起来会好一些:

    printf("Please enter your year of birth\n>> ");
    scanf_s("%d", &YoB);

    printf("Please enter the current year\n>> ");
    scanf_s("%d", &CY);
  1. 逻辑问题:

这部分逻辑有问题:

    if (Age > 18) {
        puts("You are an adult\n");
        if (Age < 18)
            puts("You are a minor\n");
    }

假设 Age< 18。我认为您希望得到输出 You are a minor。但这永远不会发生,因为要达到 if (Age < 18) 条件,您需要进入 Age > 18 条件。所以我认为你是这个意思:

// Also keep in mind that Age can be "< 0" if the user used a "bad" input.
if (Age < 0) {
    puts("You are not born yet.");
} else if (Age >= 18) {
    puts("You are an adult\n");
} else {
    puts("You are a minor\n");
}
  1. 设置 counter 一个值而不是使用循环

您可以更改此部分:

    if (Age <= 100){
        while (++counter <= 10);
    }

对此:

    if (Age <= 100) {
        counter = 10;
    }

虽然我真的不明白你怎么能输入另一个 case-arm 因为 counter 似乎只是 010 由于条件多于。我认为你的意思是这样的:

if (Age <= 100) {
    counter = Age / 10;
}

另外请使用以小写而不是大写开头的变量名,因为大写名称更适合用于结构、枚举或常量等。

总而言之,我会这样做:

#include <stdio.h>

int main()
{
    int birth_year, current_year, age;
    unsigned int tenths = 0;

    printf("Please enter your year of birth\n>> ");
    scanf_s("%d", &birth_year);

    printf("Please enter the current year\n>> ");
    scanf_s("%d", &current_year);

    age = current_year - birth_year;

    printf("Entered year of birth %d\n", birth_year);
    printf("Entered current year %d\n", current_year);
    printf("You are %d years old\n \n", age);

    if (age < 0) {
        puts("You aren't born yet.");
    } else if (age >= 18) {
        puts("You are an adult.\n");
    } else {
        puts("You are a minor.\n");
    }

    if(age <= 100){
        tenths = age % 10;
    }

    switch (tenths) {
        case 0:
            puts("You are less than 10\n");
            break;
        case 1:
            puts("you are in your tens / teens\n");
            break;
        case 2:
            puts("You are in your twenties\n");
            break;
        case 3:
            puts("You are in your thirties\n");
            break;
        case 4:
            puts("You are in your fourties\n");
            break;
        case 5:
            puts("You are in your fifties\n");
            break;
        case 6:
            puts("You are in your sixties\n");
            break;
        case 7:
            puts("You are in your seventies\n");
            break;
        case 8:
            puts("You are in your eighties\n");
            break;
        case 9:
            puts("You are in your nineties\n");
            break;
        case 10:
            puts("You are a 100+!!\n");
            break;
        default:
            puts("invalid age!");
            break;
    }
    return 0;
}