枚举数据类型

作者:追风剑情 发布于:2019-2-25 13:23 分类:Objective-C

示例

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // 定义枚举,12个月份
        enum month {january = 1, february, march, april, may, june,
            july, august, september, october, november, december};
        enum month amonth;
        int days;
        
        NSLog(@"Enter month number: ");
        scanf(" %i", &amonth);
        
        switch (amonth) {
            case january:
            case march:
            case may:
            case july:
            case august:
            case october:
            case december:
                days = 31;
                break;
            case april:
            case june:
            case september:
            case november:
                days = 30;
                break;
            case february:
                days = 28;
                break;
            default:
                NSLog(@"bad month number");
                days = 0;
                break;
        }
        
        if ( days != 0 )
            NSLog(@"Number of days is %i", days);
        
        if ( amonth == february )
            NSLog(@"...or 29 if it's a leap year2");
    }
    return 0;
}

运行测试
3333.png

标签: Objective-C

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号