| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1476 人关注过本帖
标题:如何给这个时钟加减的程序加上一个循环选择继续执行还是结束?
只看楼主 加入收藏
winglesswu
Rank: 1
等 级:新手上路
帖 子:92
专家分:0
注 册:2013-1-28
结帖率:71.88%
收藏
已结贴  问题点数:20 回复次数:11 
如何给这个时钟加减的程序加上一个循环选择继续执行还是结束?
#include<stdio.h>
main()
{
int h1, min1, sec1, a;
int h2, min2, sec2, b;
char choice;

printf("A for add\n");
printf("S for subtract\n");
printf("Please enter A or S only:");
scanf("%c", &choice);

switch (choice)
{
    case 'A':    printf("\nPlease enter the first clock in correct Military format:\n");
                 scanf("%d%d%d", &h1, &min1, &sec1);
                 printf("Clock1=%d seconds\n", a=h1*3600+min1*60+sec1);
                 printf("\nPlease enter the second clock in correct Military format:\n");
                 scanf("%d%d%d", &h2, &min2, &sec2);
                 printf("Clock2=%d seconds\n", b=h2*3600+min2*60+sec2);
                 if (a+b==86400)
                 {
                 printf("\nThen Clock3 is 00:00:00\n");
                 }
                 else
                 {
                     if (a+b>86400)
                     {
                     printf("\nThen Clock3 is %d:%d:%d hrs\n", (a+b)/3600-24, (a+b)%3600/60, (a+b)%60);
                     }
                      else
                     {
                      printf("\nThen Clock3 is %d:%d:%d hrs\n", (a+b)/3600, (a+b)%3600/60, (a+b)%60);
                     }
                 }
                 break;
     
     case 'S':   printf("\nPlease enter the first clock in correct Military format:\n");
                 scanf("%d%d%d", &h1, &min1, &sec1);
                 printf("Clock1=%d seconds\n", a=h1*3600+min1*60+sec1);
                 printf("\nPlease enter the second clock in correct Military format:\n");
                 scanf("%d%d%d", &h2, &min2, &sec2);
                 printf("Clock2=%d seconds\n", b=h2*3600+min2*60+sec2);
                 if (a-b==0)
                 {
                 printf("\nThen Clock3 is 00:00:00\n");
                 }
                 else
                    if (a-b<0)
                    {
                     printf("\nThen Clock3 is %d:%d:%dhrs\n", ( a+24*3600-b)/3600, (a+24*3600-b)%3600/60, (a+24*3600-b)%60);
                    }
                    else
                    {
                    printf("\nThen Clock3 is %d:%d:%dhrs\n", ( a-b)/3600, (a-b)%3600/60, (a-b)%60);
                    }
                 break;
                 default: printf("\nInvalid choice, please try again.\n\n");
}
                  
}
搜索更多相关主题的帖子: clock first Military correct include 
2013-02-05 10:50
心灯甚亮
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:2
帖 子:441
专家分:996
注 册:2013-1-29
收藏
得分:5 
外面套个循环(while(1)),在循环末位询问,如果选择结束,break就行了
2013-02-05 10:57
winglesswu
Rank: 1
等 级:新手上路
帖 子:92
专家分:0
注 册:2013-1-28
收藏
得分:0 
具体语句怎么写呢,我试了好几种语句都不行。

[ 本帖最后由 winglesswu 于 2013-2-5 12:24 编辑 ]
2013-02-05 11:51
dsl975708035
Rank: 3Rank: 3
来 自:平凉
等 级:论坛游侠
帖 子:101
专家分:152
注 册:2012-8-3
收藏
得分:5 
#include<stdio.h>
main()
{
    int h1, min1, sec1, a;
    int h2, min2, sec2, b;
    char choice;

    while(1)
    {
        printf("A for add\n");
        printf("S for subtract\n");
        printf("Please enter A or S only:");
        scanf("%c", &choice);

        switch (choice)
        {
        case 'A':
            printf("\nPlease enter the first clock in correct Military format:\n");
            scanf("%d%d%d", &h1, &min1, &sec1);
            printf("Clock1=%d seconds\n", a=h1*3600+min1*60+sec1);
            printf("\nPlease enter the second clock in correct Military format:\n");
            scanf("%d%d%d", &h2, &min2, &sec2);
            printf("Clock2=%d seconds\n", b=h2*3600+min2*60+sec2);
            if (a+b==86400)
            {
                printf("\nThen Clock3 is 00:00:00\n");
            }
            else
            {
                if (a+b>86400)
                {
                    printf("\nThen Clock3 is %d:%d:%d hrs\n", (a+b)/3600-24, (a+b)%3600/60, (a+b)%60);
                }
                else
                {
                    printf("\nThen Clock3 is %d:%d:%d hrs\n", (a+b)/3600, (a+b)%3600/60, (a+b)%60);
                }
            }
            break;

        case 'S':
            printf("\nPlease enter the first clock in correct Military format:\n");
            scanf("%d%d%d", &h1, &min1, &sec1);
            printf("Clock1=%d seconds\n", a=h1*3600+min1*60+sec1);
            printf("\nPlease enter the second clock in correct Military format:\n");
            scanf("%d%d%d", &h2, &min2, &sec2);
            printf("Clock2=%d seconds\n", b=h2*3600+min2*60+sec2);
            if (a-b==0)
            {
                printf("\nThen Clock3 is 00:00:00\n");
            }
            else if (a-b<0)
            {
                printf("\nThen Clock3 is %d:%d:%dhrs\n", ( a+24*3600-b)/3600, (a+24*3600-b)%3600/60, (a+24*3600-b)%60);
            }
            else
            {
                printf("\nThen Clock3 is %d:%d:%dhrs\n", ( a-b)/3600, (a-b)%3600/60, (a-b)%60);
            }
            break;
        default:
            printf("\nInvalid choice, please try again.\n\n");
        }
        getchar();
        printf("Again?(y/n)");
        if(getchar()=='n')
            break;
        fflush(stdin);
    }

}
OK的话给分,过两天给大家贺年啊

[ 本帖最后由 dsl975708035 于 2013-2-5 13:22 编辑 ]

唉,初三要停停了,舍不得啊。。
2013-02-05 13:20
yaobao
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:蒙面侠
威 望:4
帖 子:1854
专家分:4121
注 册:2012-10-25
收藏
得分:5 
#include<stdio.h>
main()
{
    int h1, min1, sec1, a;
    int h2, min2, sec2, b;
    char choice;

    while(1)
    {
        printf("A for add\n");
        printf("S for subtract\n");
        printf("R for end\n");
        printf("Please enter A or S only:");
        scanf("%c", &choice);

        switch (choice)
        {
        case 'R':return;
        case 'A':
            printf("\nPlease enter the first clock in correct Military format:\n");
            scanf("%d%d%d", &h1, &min1, &sec1);
            printf("Clock1=%d seconds\n", a=h1*3600+min1*60+sec1);
            printf("\nPlease enter the second clock in correct Military format:\n");
            scanf("%d%d%d", &h2, &min2, &sec2);
            printf("Clock2=%d seconds\n", b=h2*3600+min2*60+sec2);
            if (a+b==86400)
            {
                printf("\nThen Clock3 is 00:00:00\n");
            }
            else
            {
                if (a+b>86400)
                {
                    printf("\nThen Clock3 is %d:%d:%d hrs\n", (a+b)/3600-24, (a+b)%3600/60, (a+b)%60);
                }
                else
                {
                    printf("\nThen Clock3 is %d:%d:%d hrs\n", (a+b)/3600, (a+b)%3600/60, (a+b)%60);
                }
            }
            break;

        case 'S':
            printf("\nPlease enter the first clock in correct Military format:\n");
            scanf("%d%d%d", &h1, &min1, &sec1);
            printf("Clock1=%d seconds\n", a=h1*3600+min1*60+sec1);
            printf("\nPlease enter the second clock in correct Military format:\n");
            scanf("%d%d%d", &h2, &min2, &sec2);
            printf("Clock2=%d seconds\n", b=h2*3600+min2*60+sec2);
            if (a-b==0)
            {
                printf("\nThen Clock3 is 00:00:00\n");
            }
            else if (a-b<0)
            {
                printf("\nThen Clock3 is %d:%d:%dhrs\n", ( a+24*3600-b)/3600, (a+24*3600-b)%3600/60, (a+24*3600-b)%60);
            }
            else
            {
                printf("\nThen Clock3 is %d:%d:%dhrs\n", ( a-b)/3600, (a-b)%3600/60, (a-b)%60);
            }
            break;
        default:
            printf("\nInvalid choice, please try again.\n\n");
        }
        getchar();
        printf("Again?(y/n)");
        if(getchar()=='n')
            break;
        fflush(stdin);
    }

}

认认真真的学习,踏踏实实的走路:戒骄戒躁!!!
2013-02-05 22:04
winglesswu
Rank: 1
等 级:新手上路
帖 子:92
专家分:0
注 册:2013-1-28
收藏
得分:0 
谢谢两位的解答,但是输入第一次的时候没问题,当选择y重新输入时显示:

Then Clock3 is 11:9:59hrs
Again?(y/n)y
A for add
S for subtract
Please enter A or S only:A
Again?(y/n)A for add
S for subtract
Please enter A or S only:A

这个Again选择又出现了。
2013-02-06 09:54
心灯甚亮
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:2
帖 子:441
专家分:996
注 册:2013-1-29
收藏
得分:0 
我测试没问题啊
图片附件: 游客没有浏览图片的权限,请 登录注册
2013-02-06 10:40
winglesswu
Rank: 1
等 级:新手上路
帖 子:92
专家分:0
注 册:2013-1-28
收藏
得分:0 
谢谢心灯甚亮,请问可以把具体的嵌套写出来吗。下面是我写的:
#include<stdio.h>
main()
{
    int h1, min1, sec1, a;
    int h2, min2, sec2, b;
    char yn, choice;

    while(1)
    {
        printf("This is a program of adding or substracting time which results in another time. Have fun.\n");
        printf("\nPlease follow the instructions. Thank you!\n");
        printf("A for add\n");
        printf("S for subtract\n");
        printf("Please enter A or S only:");
        scanf("%c", &choice);

        switch (choice)
        {
        case 'A':
            printf("\nPlease enter the first clock in correct Military format:\n");
            scanf("%d%d%d", &h1, &min1, &sec1);
            printf("Clock1=%d seconds\n", a=h1*3600+min1*60+sec1);
            printf("\nPlease enter the second clock in correct Military format:\n");
            scanf("%d%d%d", &h2, &min2, &sec2);
            printf("Clock2=%d seconds\n", b=h2*3600+min2*60+sec2);
            if (a+b==86400)
            {
                printf("\nThen Clock3 is 00:00:00\n");
            }
            else
            {
                if (a+b>86400)
                {
                    printf("\nThen Clock3 is %d:%d:%d hrs\n", (a+b)/3600-24, (a+b)%3600/60, (a+b)%60);
                }
                else
                {
                    printf("\nThen Clock3 is %d:%d:%d hrs\n", (a+b)/3600, (a+b)%3600/60, (a+b)%60);
                }
            }
            break;

        case 'S':
            printf("\nPlease enter the first clock in correct Military format:\n");
            scanf("%d%d%d", &h1, &min1, &sec1);
            printf("Clock1=%d seconds\n", a=h1*3600+min1*60+sec1);
            printf("\nPlease enter the second clock in correct Military format:\n");
            scanf("%d%d%d", &h2, &min2, &sec2);
            printf("Clock2=%d seconds\n", b=h2*3600+min2*60+sec2);
            if (a-b==0)
            {
                printf("\nThen Clock3 is 00:00:00\n");
            }
            else if (a-b<0)
            {
                printf("\nThen Clock3 is %d:%d:%dhrs\n", ( a+24*3600-b)/3600, (a+24*3600-b)%3600/60, (a+24*3600-b)%60);
            }
            else
            {
                printf("\nThen Clock3 is %d:%d:%dhrs\n", ( a-b)/3600, (a-b)%3600/60, (a-b)%60);
            }
            break;
        }
        getchar();
        printf("Again?(y/n)");
        if(getchar()=='n')
            break;
        fflush(stdin);
    }

}
但是具体运行的时候会出现下面的结果:
Again?(y/n)y
This is a program of adding or substracting time which results in another time. Have fun.

Please follow the instructions. Thank you!
A for add
S for subtract
Please enter A or S only:S
Again?(y/n)This is a program of adding or substracting time which results in another time. Have fun.

Please follow the instructions. Thank you!
A for add
S for subtract
Please enter A or S only:
问题是我选了y时,出现:
This is a program of adding or substracting time which results in another time. Have fun.

Please follow the instructions. Thank you!
A for add
S for subtract
Please enter A or S only:
这时候我再选择S时,则再次出现:
Again?(y/n)This is a program of adding or substracting time which results in another time. Have fun.

Please follow the instructions. Thank you!
A for add
S for subtract
Please enter A or S only:
下面选择就没问题了。
2013-02-06 11:51
心灯甚亮
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:2
帖 子:441
专家分:996
注 册:2013-1-29
收藏
得分:0 
again(y/n)
这里你直接按了回车吧。。。
getchar()接受了"\n",你可以改成按除’n‘意外的任意键继续,n退出

[ 本帖最后由 心灯甚亮 于 2013-2-6 12:08 编辑 ]
2013-02-06 12:06
winglesswu
Rank: 1
等 级:新手上路
帖 子:92
专家分:0
注 册:2013-1-28
收藏
得分:0 
您说的对,按了回车键就等于选择了y。那么有没有办法输入y继续循环,而不用回车键呢?谢谢。
2013-02-06 12:22
快速回复:如何给这个时钟加减的程序加上一个循环选择继续执行还是结束?
数据加载中...
 
   



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

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