标题:求再次输入循环思路???
只看楼主
hacker梦魇
Rank: 2
等 级:论坛游民
帖 子:40
专家分:42
注 册:2015-10-29
得分:0 
回复 8楼 TonyDeng
我会好好的思考你这句话的
2015-12-05 21:52
mj0011
Rank: 2
等 级:论坛游民
帖 子:24
专家分:39
注 册:2014-12-23
得分:3 
把switch语句包含在一个函数里然后在输入大于7时调用这个函数
2015-12-06 02:03
ehszt
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:40
帖 子:1728
专家分:3216
注 册:2015-12-2
得分:0 
#include<stdio.h>
 int main()
 {
   int iDay= 0;
   do
   {printf("please input a number:\n");
   scanf("%d",&iDay);
   switch(iDay)
   {
     case 1:
     case 2:
     case 3:
     case 4:
     case 5:printf("Today to work hark\n");break;
     case 6:printf("Today stay at home to sleep\n");break;
     case 7:printf("Go shopping with friends\n");break;
     case 8:break;                 //选8可以退出,非1-8重选
     default:printf("Error\n");
    }
 }while(iDay!=8);
   return 0;
}
//测试正确

[此贴子已经被作者于2015-12-6 09:51编辑过]

2015-12-06 09:49
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
得分:0 
回复 13楼 ehszt
你试试输入的不是数字会怎样?

授人以渔,不授人以鱼。
2015-12-06 12:16
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
得分:0 
如果用低版本的vs或其他编译器,把printf_s()和scanf_s()、gets_s()函数后面的_s删掉。

程序代码:
/*
http://bbs.bccn.net/thread-459912-1-1.html
Version 1, 2015.12.06.
*/

#include <stdio.h>
#include <conio.h>

int main(void)
{
    int iDay;
    int iScanReturn;
    int bContinue;
    char cBuffer[1024];

    do
    {
        bContinue = 0;
        printf_s("please input a number: ");
        iScanReturn = scanf_s("%d", &iDay);
        if (iScanReturn != 1)
        {
            gets_s(cBuffer);
            bContinue = 1;
        }
        switch (iDay)
        {
            case 1:
            case 2:
            case 3:
            case 4:
                break;
            case 5:
                printf_s("Today to work hark\n");
                break;
            case 6:
                printf_s("Today stay at home to sleep\n");
                break;
            case 7:
                printf_s("Go shopping with friends\n");
                break;
            default:
                printf_s("Error\n");
                bContinue = 1;
                break;
        }
    } while (bContinue);

    _getch();
    return 0;
}

授人以渔,不授人以鱼。
2015-12-06 13:15
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
得分:0 
程序代码:
/*
http://bbs.bccn.net/thread-459912-1-1.html
Version 2, 2015.12.06.
*/

#include <stdio.h>
#include <conio.h>

int main(void)
{
    int iDay;
    int iScanReturn;
    int bContinue = 1;
    char cBuffer[1024];

    while (bContinue)
    {
        bContinue = 0;
        printf_s("please input a number: ");
        iScanReturn = scanf_s("%d", &iDay);
        if (iScanReturn != 1)
        {
            gets_s(cBuffer);
            bContinue = 1;
        }
        switch (iDay)
        {
        case 1:
        case 2:
        case 3:
        case 4:
            break;
        case 5:
            printf_s("Today to work hark\n");
            break;
        case 6:
            printf_s("Today stay at home to sleep\n");
            break;
        case 7:
            printf_s("Go shopping with friends\n");
            break;
        default:
            printf_s("Error\n");
            bContinue = 1;
            break;
        }
    }

    _getch();
    return 0;
}

授人以渔,不授人以鱼。
2015-12-06 13:21
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
得分:0 
程序代码:
/*
http://bbs.bccn.net/thread-459912-1-1.html
Version 3, 2015.12.06.
*/

#include <stdio.h>
#include <conio.h>

const char* Schedule[] = {
    "",
    "",
    "",
    "",
    "Today to work hark",
    "Today stay at home to sleep",
    "Go shopping with friends"
};

int main(void)
{
    int iDay;
    int iScanReturn;
    int bContinue;
    char cBuffer[1024];

    do
    {
        printf_s("please input a number: ");
        iScanReturn = scanf_s("%d", &iDay);
        if ((iScanReturn == 1) && (iDay >= 1) && (iDay <= 7))
        {
            printf_s("%s\n", Schedule[iDay - 1]);
            bContinue = 0;
        }
        else
        {
            gets_s(cBuffer);
            printf_s("Error\n");
            bContinue = 1;
        }
    } while (bContinue);

    printf_s("\nPress any key to continue...");
    _getch();
    return 0;
}

授人以渔,不授人以鱼。
2015-12-06 13:36
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
得分:0 
程序代码:
/*
http://bbs.bccn.net/thread-459912-1-1.html
Version 4, 2015.12.06.
*/

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

const char* Schedule[] = {
    "",
    "",
    "",
    "",
    "Today to work hark",
    "Today stay at home to sleep",
    "Go shopping with friends"
};

int main(void)
{
    int iDay;
    int iScanReturn;
    int bContinue;
    char cBuffer[1024];

    do
    {
        printf_s("please input a number: ");
        iScanReturn = scanf_s("%d", &iDay);
        if ((iScanReturn == 1) && (iDay >= 1) && (iDay <= _countof(Schedule)))
        {
            printf_s("%s\n", Schedule[iDay - 1]);
            bContinue = 0;
        }
        else
        {
            gets_s(cBuffer);
            printf_s("Error\n");
            bContinue = 1;
        }
    } while (bContinue);

    printf_s("\nPress any key to continue...");
    _getch();
    return EXIT_SUCCESS;
}

授人以渔,不授人以鱼。
2015-12-06 13:42
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
得分:0 
所谓“多写”,主要是指对一个程序多琢磨,写多个不同实现方式的版本,不是写很多很多不同的题目。你自己比较一下上面几个版本的程序各有什么不同吧,尤其是3、4版本,改动其实仅有一处,但区别却是很大的——这也是根据坛上之前有个帖子的问题而特意写这么两个版本。

[此贴子已经被作者于2015-12-6 13:58编辑过]


授人以渔,不授人以鱼。
2015-12-06 13:57
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
得分:0 
搞明白了版本3、4的手法,你就知道菜单怎么写。

授人以渔,不授人以鱼。
2015-12-06 14:02



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-459912-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023717 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved