循环

while循环:

 先判断再执行

 例:头文件“”一般是自己定义的,<>是C语言自带的;

<>在系统目录下找
""在当前目录下找


#include<iostream>
#include "stdafx.h"
#include "iostream"
#include<string>

using namespace std;

//
使用循环实现三次密码输入错误退出系统 string password;//密码 int i = 0; while (i<3) { cout << "请输入密码: " << endl; cin >> password; if (password == "123") { cout << "密码正确,登录成功" << endl; break; } else { if (i == 2) { cout << "输入超过三次,程序自动退出" << endl; break; } cout << "密码错误,请重新输入" << endl; i++; } } system("pause");

 

 

//使用时间做种子,每次产生不一样的随机数
    int start = 5;
    int end = 15;
    srand(unsigned(time(NULL)));
    for (int i = 0; i < 10; i++)
    {
        cout << int(start + (end - start)*rand() / (RAND_MAX + 1.0)) << endl;
    }

    int    hp1 = 100;//1号的生命值
    int hp2 = 100;
    int attack1 = 0; //1号的攻击力
    int attack2 = 0;
    int randNum;

    srand(time(NULL));

    while (hp1 > 0 && hp2 > 0)
    {
        //1.模拟玩家出招,随机数为奇数1号先
        randNum = rand();
        if (randNum % 2 == 1)//奇数
        {
            attack1 = (int)(5 + 10 * rand() / (RAND_MAX + 1.0));
            attack2 = (int)(5 + 10 * rand() / (RAND_MAX + 1.0));

            hp2 -= attack1;
            hp1 -= attack2;

        }
        else//偶数
        {
            attack1 = (int)(5 + 10 * rand() / (RAND_MAX + 1.0));
            attack2 = (int)(5 + 10 * rand() / (RAND_MAX + 1.0));
            hp1 -= attack2;
            hp2 -= attack1;

        }
    }
    if (hp1 <= 0)
    {
        cout << "1输了" << endl;
    }
    else
    {
        cout << "2输了" << endl;
    }

 

 

 for 循环:

 

    //循环输入6个月工资,求平均工资
    double salary = 0;
    double sumsalary = 0;
    for (int i = 1; i <= 6; i++)
    {
        cout << "请输入"<< i <<"个月薪水" << endl;
        cin >> salary;
        sumsalary += salary;
    }
    double avgsalry = sumsalary / 6;
    cout << "6个月的平均工资是:" << avgsalry << endl;
//输入1997年7月的月历,7.1日星期二;
    int day = 31;
    int dayofWeek = 2;
    cout << "1997年7月日历如下: " << endl;
    cout << setw(8) << "日	一	二	三	四	五	六" << endl;
    //打印周几
    cout << "		" ;
    for (int i = 1; i <= day; i++)
    {
        cout << i;
        if ((i+dayofWeek)%7 == 0)
        {
            cout << "
";
        }
        else
        {
            cout << "	";
        }
    }
    cout << endl;
    

练习:

 

//第一题
    //上半部
    for (int i = 1; i < 5; i++)
    {    
        //打印空格
        for (int j = 0; j <= 4-i; j++)
        {
            cout << " ";
        }
        //打印星号
        for (int k = 1; k <= 2*i-1; k++)
        {
            cout << "*";
        }
        cout << endl;
    }
    //下半部
    for (int l = 1; l < 4; l++)
    {
        //打印空格
        for (int m = 0; m <= l; m++)
        {
            cout << " ";
        }
        //打印星号
        for (int n = 1; n <= 7-2*l; n++)
        {
            cout << "*";
        }
        cout << endl;
    }
//第二题
    //上半部
    for (int i = 1; i < 5; i++)
    {
        //打印空格
        for (int j = 0; j <= 4 - i; j++)
        {
            cout << " ";
        }
        //打印星号
        for (int k = 1; k <= 2 * i - 1; k++)
        {
            cout << char('A'+i-1);
        }
        cout << endl;
    }
    //下半部
    for (int l = 1; l < 4; l++)
    {
        //打印空格
        for (int m = 0; m <= l; m++)
        {
            cout << " ";
        }
        //打印星号
        for (int n = 1; n <= 7 - 2 * l; n++)
        {
            cout << char('E'+l-1);
        }
        cout << endl;
    }
//第三题
    //上半部
    for (int i = 1; i < 5; i++)
    {
        //打印空格
        for (int j = 0; j <= 4 - i; j++)
        {
            cout << " ";
        }
        //打印星号
        for (int k = 1; k <= 2 * i - 1; k++)
        {
            if (k == 1 || k == 2 * i - 1)
            {
                cout << '*';
            }
            else
            {
                cout << ' ';
            }
        }
        cout << endl;
    }
    //下半部
    for (int l = 1; l < 4; l++)
    {
        //打印空格
        for (int m = 0; m <= l; m++)
        {
            cout << " ";
        }
        //打印星号
        for (int n = 1; n <= 7 - 2 * l; n++)
        {
            if (n == 1 || n == 7 - 2*l)
            {
                cout << '*';
            }
            else
            {
                cout << ' ';
            }
            
        }
        cout << endl;
    }

1

原文地址:https://www.cnblogs.com/Manuel/p/11480709.html