C语言学习笔记[27]:循环语句do...while②

  • 编写代码,演示多个字符从两边移动,向中间汇聚

//题目的意思是将使用如下方法打印
//w####################!
//we##################!!
//wel################!!!
//...
//welcome to my world!!!
//每次从左边和右边分别打印出一个字符,其余用 # 代替,直到所有字符都被打印出来

#include <stdio.h>
#include <string.h>
#include <windows.h>

int main()
{
    char arr1[] = "welcome to my world!!!";
    char arr2[] = "######################";
    int left = 0;
    int right = strlen(arr1) - 1;
    do
    {
        arr2[left] = arr1[left];
        arr2[right] = arr1[right];
        printf("%s\n", arr2);
        Sleep(1000);
        system("cls");
        left++;
        right--;
    } while (left <= right);
    return 0;
}
  •  编写代码,模拟用户登陆场景,并且只能登陆三次(只允许输入三次密码,密码正确则提示登陆成功,如果三次均错误,则退出程序)
#include <stdio.h>
#include <string.h>
#include <windows.h>

int main()
{
    int i = 0;
    //假设正确的密码是 123456
    char password[10] = {0};
    do
    {
        printf("请输入密码:");
        scanf("%s", password);
        if (strcmp(password, "123456") == 0)
        {
            printf("密码正确!");
            break;

        }
        else
        {
            printf("密码错误!");
        }
        i++;
    } while (i < 3);
    if (i == 3)
    {
        printf("密码错误次数过多!");
    }
    return 0;
}
  • 猜数字游戏
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void menu()
{
    printf("**********************************\n");
    printf("*********** 1.play **********\n");
    printf("*********** 0.exit **********\n");
    printf("**********************************\n");
}
//RAND_MAX--rand函数能返回随机数的最大值。
void game()
{
    int random_num = rand()%100+1;
    int input = 0;
    while(1)
    {
        printf("请输入猜的数字>:");
        scanf("%d", &input);
        if(input > random_num)
        {
            printf("猜大了\n");
        }
        else if(input < random_num)
        {
            printf("猜小了\n");
        }
        else
        {
            printf("恭喜你,猜对了\n");
            break;
        }
    }
}
int main()
{
    int input = 0;
    srand((unsigned)time(NULL));
    do
    {
        menu();
        printf("请选择>:");
        scanf("%d", &input);
        switch(input)
        {
            case 1:
                game();
                break;
            case 0:
                break;
            default:
                printf("选择错误,请重新输入!\n");
                break;
        }
    }while(input);
    return 0;
}

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值