| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 381 人关注过本帖
标题:帮帮忙 看看这个程序代码哪有问题?自己没想通!!
只看楼主 加入收藏
牙齿哥
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2011-5-30
结帖率:100%
收藏
已结贴  问题点数:5 回复次数:3 
帮帮忙 看看这个程序代码哪有问题?自己没想通!!
程序代码:
写了个判断密码的代码,用户名没有判断。
题目要求:
编写一个密码登录程序,程序中将提示用户输入用户名和密码。如果密码有误,提示从新输入,
直到输入正确或连续3次输入错误后退出循环,并显示相应信息。

#define PASSWD "yachige"  默认密码
输入密码后有3次机会从新输入密码,3次都输入错误错误退出






#include<stdio.h>
#include<string.h>//比较字符串大小
#define PASSWORD "yachige"
int main(void)
{
    char user[20],password[15];
    int flag;
    int i=3,b=3;
    printf("Enter the use:");
    scanf("%s",&user);
    printf("\nEnter the passwd:");
    scanf("%s",&password);
    flag = strcmp(PASSWORD,password);//对比2个字符串,相同返回0,不相同(大于返回正整数,小于返回负整数)。
    while(flag)//循环直到输入次数变0或者输入正确。
    {  
        if(i<=1)
        break;
        if(i>0)
            {
                printf("The passwd is wory.Please enter the right passwd.");
                printf("You have %d times opportunite.\n",--i);
                scanf("%s",&password);
                flag = strcmp(PASSWORD,password);                   
            }
       
    }
    if(flag)
    printf("The prgrammse is end\n");
    else printf("Willcome the word:%s!\n",user);
    return 0;
}
上面while循环本来只循环输入3次, 那么我想第一个if语句应该是if(i<=0)才对啊   为什么if(i<=0)后他要输入4次才结束而用if(i<=1)就这输入3次。。。   一直没想明白。。应该是if(i<=0)啊!!。。。。     各位帮帮忙看看。。。。      还有就是有什么地方能够改进吗?
搜索更多相关主题的帖子: 输入密码 用户名 
2011-05-31 08:15
hjywyj
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:3
帖 子:1114
专家分:2611
注 册:2010-4-14
收藏
得分:0 
#define PASSWORD "yachige"
char user[20],password[15];
    int flag;
    int i=0;
    printf("Enter the use:");
    scanf("%s",&user);
    printf("\nEnter the passwd:");
    scanf("%s",&password);
    while(i<3)//循环直到输入次数变0或者输入正确。
    {if(strcmp(PASSWORD,password)==0)
    {printf("The password is right");
    break;}
    else
        {printf("The passwd is wory.Please enter the right passwd.");
                printf("You have %d times opportunite.\n",3-i-1);
                scanf("%s",&password);if(i++>=3)
               {printf("The prgrammse is end\n"); break;}}
      
    }
2011-05-31 08:26
qq1023569223
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:湖南科技大学
等 级:贵宾
威 望:26
帖 子:2753
专家分:13404
注 册:2010-12-22
收藏
得分:4 
程序代码:
#include<stdio.h>
#include<string.h>//比较字符串大小
#define PASSWORD "yachige"
int main(void)
{
    char user[20],password[15];
    int flag;
    int i=2,b=3;  //while前你已经输入了一次,i=2
    printf("Enter the use:");
    scanf("%s",user);  //???
    printf("\nEnter the passwd:");
    scanf("%s",password);  //???
    flag = strcmp(PASSWORD,password);//对比2个字符串,相同返回0,不相同(大于返回正整数,小于返回负整数)。
    while(flag)//循环直到输入次数变0或者输入正确。
    { 
        if(i<=0)  //???
        break;
        if(i>0)
            {
                printf("The passwd is wory.Please enter the right passwd.");
                printf("You have %d times opportunite.\n",--i);
                scanf("%s",&password);
                flag = strcmp(PASSWORD,password);                  
            }
      
    }
    if(flag)
    printf("The program is end\n");
    else printf("Welcome the word:%s!\n",user);
    return 0;
}


[ 本帖最后由 qq1023569223 于 2011-5-31 08:29 编辑 ]

   唯实惟新 至诚致志
2011-05-31 08:28
因为曾今年少
Rank: 2
等 级:论坛游民
帖 子:131
专家分:62
注 册:2011-4-13
收藏
得分:1 
程序代码:
while(flag)//循环直到输入次数变0或者输入正确。
    { 
        if(i<=1)
        break;
        if(i>0)
            {
                printf("The passwd is wory.Please enter the right passwd.");
                printf("You have %d times opportunite.\n",--i);
                scanf("%s",&password);
                flag = strcmp(PASSWORD,password);                  
            }
      
    }

呵呵呵 我可以说几句么 因为你的 i赋值为3  在上述程序段中if 判断的跳出条件是i<=0话 你减一减就会发现i为3的时候就循环一次  然后是2  再是1这就循环三次啦  如果你让它等于零的话 还有一次哟  是不是
2011-05-31 09:00
快速回复:帮帮忙 看看这个程序代码哪有问题?自己没想通!!
数据加载中...
 
   



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

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