簡體   English   中英

如何正確使用開關盒?

[英]How do I use switch case properly?

誰能告訴我我使用的開關盒是否正確? 因為當我輸入其他數字時,它的輸出總是一月。

#include <stdio.h>


int main()
{
    int month, date, year;

    printf("Enter Month Number: ");
    scanf("%d", &month);

    if (month > 12){
        printf("Invalid Month! please choose a number below 12");
    }
    else{
        printf("\nEnter Date Number: ");
        scanf("%d", &date);

        if (month = 1, 3, 5, 7, 8, 10, 12){
            if (date > 31){
                printf("\nInvalid Date! please choose a number below 31");
            }
            else{
                printf("\nEnter Year: ");
                scanf("%d", &year);
                if (year > 9999){
                    printf("\nPlease make sure your year is correct!");
                }
                else if (year < 1000){
                    printf("\nPlease make sure your year is correct!");
                }
                else{
                    switch(month){
                        case 1:
                            printf("\nJanuary %d, %d", date, year);
                            break;
                        case 3:
                            printf("\nMarch %d, %d", date, year);
                            break;
                        case 5:
                            printf("\nMay %d, %d", date, year);
                            break;
                        case 7:
                            printf("\nJuly %d, %d", date, year);
                            break;
                        case 8:
                            printf("\nAug %d, %d", date, year);
                            break;
                        case 10:
                            printf("\nOctober %d, %d", date, year);
                            break;
                        case 12:
                            printf("\nDecember %d, %d", date, year);
                            break;
                    }
                }
            }
        }
        else if (month = 4, 6, 9, 11){
            if (date > 30){
                printf("\nInvalid Date! please choose a number below 30");
            }
            else{
                printf("\nEnter Year: ");
                scanf("%d", &year);
                if (year > 9999){
                    printf("\nPlease make sure your year is correct!");
                }
                else if (year < 1000){
                    printf("\nPlease make sure your year is correct!");
                }
                else{
                    switch(month){
                        case 4:
                            printf("\nApril %d, %d", date, year);
                            break;
                        case 6:
                            printf("\nJne %d, %d", date, year);
                            break;
                        case 9:
                            printf("\nSeptember %d, %d", date, year);
                            break;
                        case 11:
                            printf("\nNovember %d, %d", date, year);
                            break;
                    }
                }
            }


        }
        else if (month = 2){
            if (date > 29){
                printf("\nInvalid Date! please choose a number below 29");
            }
            else{
                printf("\nEnter Year: ");
                scanf("%d", &year);
                if (year > 9999){
                    printf("\nPlease make sure your year is correct!");
                }
                else if (year < 1000){
                    printf("\nPlease make sure your year is correct!");
                }
                else{
                    printf("\nFebruary %d, %d", date, year);

                }
            }
        }


    }

return 0;
}

幾個問題...

  1. =賦值運算符。 你想要相等運算符==
  2. if (month = 4, 6, 9, 11)只是不正確的語法。 正如頂部評論中提到的,這將通過switch/case塊或if ((month == 4) || (month == 6) || (month == 9) || (month == 11))
  3. 代碼不必要地冗長。 擁有一個描述月份(日期和月份字符串)的struct及其數組大大簡化了代碼。
  4. 在進行檢查之前提示用戶輸入所有三個值也可以簡化代碼。

這是一個簡化版本。 是這樣注釋的:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

struct month {
    int days;                           // (max) number of days in month
    const char *str;                    // month string
};

struct month months[12] = {
    { 31, "January" },
    { 29, "February" },
    { 31, "March" },
    { 30, "April" },
    { 31, "May" },
    { 30, "June" },
    { 31, "July" },
    { 31, "August" },
    { 30, "September" },
    { 31, "October" },
    { 30, "November" },
    { 31, "December" },
};

// asknum -- prompt user for number within a given range
int
asknum(const char *prompt,int lo,int hi)
{
    char buf[100];
    char *cp;
    long val;

    // prompt the user
    printf("Enter %s (%d to %d): ",prompt,lo,hi);
    fflush(stdout);

    // get user's response -- exit on EOF
    if (fgets(buf,sizeof(buf),stdin) == NULL)
        exit(7);

    // check for empty string
    if (buf[0] == '\n') {
        printf("No value specified\n");
        exit(8);
    }

    // decode the value
    errno = 0;
    val = strtol(buf,&cp,10);

    // check for error detected by strtol
    if (errno != 0) {
        printf("Invalid number\n");
        exit(9);
    }

    // check for syntax error
    if (*cp != '\n') {
        printf("Invalid syntax\n");
        exit(10);
    }

    // check range of number
    if ((val < lo) || (val > hi)) {
        printf("Number out of range -- must be between %d and %d\n",
            lo,hi);
        exit(11);
    }

    return val;
}

// isleap -- decide if it's a leap year
int
isleap(int year)
{
    int leap = 0;

    do {
        if ((year % 4) != 0)
            break;

        if ((year % 400) == 0) {
            leap = 1;
            break;
        }

        if ((year % 100) == 0)
            break;

        leap = 1;
    } while (0);

    return leap;
}

int
main(void)
{
    int month, date, year;
    int days;
    int leap = 0;

    month = asknum("Month Number",1,12);

    // locate the month descriptor
    const struct month *mon = &months[month - 1];

    date = asknum("Date Number",1,mon->days);
    year = asknum("Year",1000,9999);

    // get days in the month
    days = mon->days;

    // special case for february
    if (month == 2) {
        leap = isleap(year);
        if (! leap)
            days -= 1;
    }

    // check date against number of days in the month
    if (date > days) {
        printf("%s only has %d days\n",mon->str,days);
        exit(2);
    }

    // print the date
    printf("%s %d, %d\n",mon->str,date,year);

    return 0;
}

暫無
暫無

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

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