• c 语言 限制用户的选项(菜单浏览)


    1、

    #include <stdio.h>
    
    int main(void)
    {
        char choice;
        
        printf("a) xxxx    b) yyyy
    ");
        printf("c) zzzz    q) quit.
    ");
        printf("input your choice: ");
        
        while((choice = getchar()) != 'q')
        {
            switch(choice)
            {
                case 'a': printf("choose a.
    "); break;
                case 'b': printf("choose b.
    "); break;
                case 'c': printf("choose c.
    "); break;
                default: printf("input errer, the range is a b c q
    "); break;    
            }
            while(getchar() != '
    ')
                continue;
            printf("
    
    a) xxxx    b) yyyy
    ");
            printf("c) zzzz    q) quit.
    ");
            printf("input your choice: ");
        }    
        
        return 0;
    } 

    2、

    #include <stdio.h>
    
    char get_choice(void);
    
    int main(void)
    {
        char choice;
        while((choice = get_choice()) != 'q')
        {
            switch(choice)
            {
                case 'a': printf("you choose a.
    
    "); break;
                case 'b': printf("you choose b.
    
    "); break;
                case 'c': printf("you choose c.
    
    "); break;
                default: printf("grogramm ereor.
    
    "); break;    
            }    
        }    
        
        return 0;
    } 
    
    char get_choice(void)
    {
        char ch;
        printf("a) xxx    b) yyyy
    ");
        printf("c) zzz    q) quit
    ");
        printf("your choice: ");
        
        ch = getchar();
        while(getchar() != '
    ')
            continue;
        while(ch < 'a' || ch > 'c' && ch != 'q')
        {
            printf("the range is a b c and q, try again: ");
            ch = getchar();
            while(getchar() != '
    ')
                continue;
        }
    
        return ch;
    }

    3、

    #include <stdio.h>
    
    char get_choice(void);
    
    int main(void)
    {
        char choice;
        while((choice = get_choice()) != 'q')
        {
            switch(choice)
            {
                case 'a': printf("you choose a.
    
    "); break;
                case 'b': printf("you choose b.
    
    "); break;
                case 'c': printf("you choose c.
    
    "); break;
                default: printf("grogramm ereor.
    
    "); break;    
            }    
        }    
        
        return 0;
    } 
    
    char first_choice(void);
    char get_choice(void)
    {
        char ch;
        printf("a) xxx    b) yyyy
    ");
        printf("c) zzz    q) quit
    ");
        printf("your choice: ");
        
        ch = first_choice();
    
        while(ch < 'a' || ch > 'c' && ch != 'q')
        {
            printf("the range is a b c and q, try again: ");
            ch = first_choice();
    
        }
    
        return ch;
    }
    
    char first_choice(void)
    {
        char ch;
        
        ch = getchar();
        while(getchar() != '
    ')
            continue;
        
        return ch;
    }

  • 相关阅读:
    Linux踩坑填坑记录
    Scala安装后,在IDEA中配置
    Centos 搭建Hadoop
    conductor FAQ
    conductor Workflow Metrics
    conductor APIs
    Extending Conductor
    conductor任务域
    Conductor Task Workers
    Conductor Server
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/15192255.html
Copyright © 2020-2023  润新知