簡體   English   中英

switch 語句問題 c 語言

[英]issue with switch statement c language

所以這是我正在處理的代碼。 一切都很好,除了我需要將表殼與計數器相匹配,這樣我才能設法顯示所需的 output“你是十幾歲/十幾歲”。 我嘗試了很多不同的方法,但我做對了。 但是,我認為問題在條件之內。 有小費嗎? 注:本人只是本學期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. 添加\nprintf (小的 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 我認為您希望擁有 output 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 -arms 因為由於上述情況, 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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM