zoukankan      html  css  js  c++  java
  • 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;
    }

  • 相关阅读:
    Android ListView带CheckBox实现单选
    android 登录和设置IP/端口功能
    html5 10大html5前端框架
    Html5 8个强大的基于Bootstrap的CSS框架
    Android 探究 LayoutInflater setFactory
    Android onLoadFinished与onLoaderReset
    Android android.database.CursorIndexOutOfBoundsException:Index -1 requested, with a size of 1
    Android 中AIDL的使用与理解
    Android Studio查看android源码
    ArrayList和LinkedList的用法区别:
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/15192255.html
Copyright © 2011-2022 走看看