C#基础知识及题目练习 Day4 循环

一、while循环
语法结构:
while(循环条件)//成绩大于90
{
    循环体;
}
执行过程:程序运行到while处的时候,先判断循环条件是否成立,如果成立(循环条件为true),则执行大括号中的循环体;
循环体执行完以后,再判断循环条件是否成立,如果仍然成立,则继续执行循环体,直到循环条件不再成立为止。
while循环中,总有这么一句话,使循环条件终有一天不再成立;如果没有这样一句话,则程序会陷入死循环。


二、do-while
语法结构:
do
{
    循环体;
}
while(循环条件);
执行过程:程序运行到do处,先执行大括号所在的循环体,然后判断while中的循环条件是否满足,如果满足,就执行
循环体,依次往复;如果循环条件不满足,则直接跳出do-while循环。
特点:不管循环条件是否满足,循环体都会执行一遍

三、for循环
语法结构:
for(表达式1;表达式2;表达式3)
{
    循环体;
}

表达式1:声明的循环变量,它用来标识当前循环了几次了(int i=0;)
表达式2:循环条件 (i<10;)
表达式3:改变循环条件的代码,使循环条件终有一天不再成立。(i++;)
执行过程:程序运行到for循环处,先执行表达式1,声明一个变量用于记录循环的次数,然后
执行表达式2,判断循环条件是否成立,
如果条件成立,执行循环体,执行完以后,执行表达式3,
对表达式1中声明的变量进行递增,再执行表达式2,判断循环条件是否成立,如果成立,则继续执行循环体,依次往复。
如果条件不成立,则跳出for循环。


练习一:明天小兰就要登台演出了,
            老师说再把明天演出的歌曲唱一遍,如果满意,小兰就可以回家了,
            否则就需要再练习一遍,直到老师满意为止(y/n).
            
练习二:让用户输入账号和密码,只要不对,就一直提示重新输入
练习三:打印1-10
练习四:求1-100所有整数的和  偶数的和   奇数的和
练习五:找出100-999间的水仙花数
练习六:使用for循环打印乘法口诀表
练习七:让用户输入一个数字,显示下面的内容:添加try-catch
                            请输入一个值:6
                            0 + 6 = 6
                            1 + 5 = 6
                            2 + 4 = 6
                            3 + 3 = 6
                            4 + 2 = 6
                            5 + 1 = 6
                            6 + 0 = 6

练习八:要求输入学生人数,循环录入每个学生的年龄,并计算平均年龄,如果遇到负数或大于100的数,则停止输入并报错 添加try-catch
        练习九:1~100之间的整数相加,一直累加到大于20,打印出该数,提示:累加到{0}时,结果大于20
        练习十:用while continue实现1-100之间的除了能被7整除之外所有整数的和
        练习十一:用for continue实现1-100之间的除了能被7整除之外所有整数的和
        练习十二:找出100以内所有素数(只能被1和数字本身整除的数)
        
        i=2    2
        i=3    3 (2,3)
        i=4 2,3,4   能被2整除 break  
        i=5 2,3,4,5
        ...
        i=100 2,3,4,.....100
        
        练习十三:要求用户输入两个数字,使用三元表达式求出两个数的最大值  添加try-catch
        练习十四:要求用户输入性别,男--打印出,欢迎先生光临,女--打印出欢迎女士光临

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Day5zy_machenxi
{
    class Program
    {
        static void Main(string[] args)
        {
            #region 练习一:明天小兰就要登台演出了
            Console.WriteLine("1.小兰就要登台演出:");
            string answer = ""; //老师的回答
            do
            {
                Console.WriteLine("老师,我唱了一遍,你满意吗?");
                answer = Console.ReadLine(); //老师的回答
            } while (answer == "no");
            if (answer == "yes")
            {
                Console.WriteLine("很好,放学吧!");
            }
            #endregion

            #region 练习二:让用户输入账号和密码,只要不对,就一直提示重新输入
            string userName = "";   //账号
            string pwd = "";    //密码
            Console.WriteLine("\n\n2.请输入账号:(aaa)");
            userName = Console.ReadLine();
            Console.WriteLine("请输入密码:(111)");
            pwd = Console.ReadLine();   //密码
            while (userName != "aaa" || pwd != "111")
            {
                if (userName != "aaa")
                {
                    Console.WriteLine("请重新输入账号:");
                    userName = Console.ReadLine();
                }
                else if (pwd != "111")
                {
                    Console.WriteLine("请重新输入密码:");
                    pwd = Console.ReadLine();
                }

            } 
            #endregion

            #region 练习三:打印1-10
            Console.WriteLine("\n\n3.打印1-10:");
            for (int i = 0; i < 10; i++)
            {
                Console.Write(i + 1);
            }
            Console.WriteLine("");
            #endregion

            #region 练习四:求1-100所有整数的和  偶数的和   奇数的和
            int s = 0;
            int oushu = 0;
            int jishu = 0;
            for (int i = 1; i <= 100; i++)
            {
                s += i;
                if (i % 2 == 0) oushu += i;
                else jishu += i;
            }
            Console.WriteLine("\n\n4.1-100所有整数的和是{0},偶数的和是{1},奇数的和是{2}。", s, oushu, jishu);
            #endregion

            #region 练习五:找出100-999间的水仙花数(立方和)
            Console.Write("\n\n5.100-999间的水仙花数有:");
            for (int i = 100; i <= 999; i++)
            {
                if (Math.Pow((i / 100), 3) + Math.Pow((i / 10 % 10), 3) + Math.Pow((i % 10), 3) == i)
                {
                    Console.Write("{0}  ", i);
                }
            }
            #endregion

            #region 练习六:输出九九乘法表
            Console.WriteLine("\n\n6.九九乘法表");
            for (int i = 1; i < 10; i++)
            {
                for (int j = i; j < 10; j++)
                {
                    Console.Write("{0}*{1}={2}\t", i, j, i * j);
                }
                Console.WriteLine("");
            }
            #endregion

            #region 练习七:让用户输入一个数字,显示下面的内容:添加try-catch
            Console.WriteLine("\n\n7.请输入一个值:");
            try
            {
                int num = int.Parse(Console.ReadLine());
                for(int i = 0; i <= num; i++)
                {
                    Console.WriteLine("{0}+{1}={2}", i, num - i,num);
                }
            }
            catch
            {
                Console.WriteLine("格式输入错误。");
            }
            #endregion

            #region 练习八:要求输入学生人数,循环录入每个学生的年龄,并计算平均年龄,如果遇到负数或大于100的数,则停止输入并报错 添加try-catch
            try
            {
                Console.WriteLine("\n\n8.请输入学生的个数:");
                int n = int.Parse(Console.ReadLine());
                int s2 = 0;
                for(int i=0;i<n;i++)
                {
                    Console.WriteLine("请输入第{0}个学生的年龄", i+1);
                    int age = int.Parse(Console.ReadLine());
                    if (age >= 0 && age <=100)
                    {
                        s2 += age;
                    }
                    else
                    {
                        Console.WriteLine("年龄不在范围内!");
                        break;
                    }
                }
                Console.WriteLine("五个人的平均年龄为{0}", s2 / n);
            }
            catch
            {
                Console.WriteLine("输入格式错误。");
            }
            #endregion

            #region 练习九:1~100之间的整数相加,一直累加到大于20,打印出该数,提示:累加到{0}时,结果大于20
            int num1 = 0;
            for (int i = 1; i <= 100; i++)
            {
                num1 += i;
                if (num1 > 20)
                {
                    Console.WriteLine("\n\n9.累加到{0}时,结果大于20",i);
                    break;
                }
            }
            #endregion

            #region 练习十:用while continue实现1-100之间的除了能被7整除之外所有整数的和
            int i1 = 1;
            int s4 = 0;
            while (i1 <= 100)
            {
                if (i1 % 7 == 0)
                {
                    i1++;
                    continue;
                }
                s4 += i1;
                i1++;
            }
            Console.WriteLine("\n\n10.(while continue)1-100之间的除了能被7整除之外所有整数的和为{0}", s4);
            #endregion

            #region 练习十一:用for continue实现1-100之间的除了能被7整除之外所有整数的和
            int s5 = 0;
            for(int i=1; i<= 100;i++)
            {
                if (i % 7 == 0)
                {
                    continue;
                }
                s5 += i;
            }
            Console.WriteLine("\n\n11.(for continue)1-100之间的除了能被7整除之外所有整数的和为{0}", s5);
            #endregion

            #region 练习十二:找出100以内所有素数(只能被1和数字本身整除的数)
            Console.Write("\n\n12.100以内所有素数:");
            
            for (int i = 2; i <= 100; i++)
            {
                bool flag = true;
                for (int j = 2; j <= Math.Sqrt(i); j++)
                {
                    if (i % j == 0)
                    {
                        flag = false;
                    }   
                }
                if(flag==true)
                {
                    Console.Write("{0}  ",i);
                }
            }

            #endregion

            #region 练习十三:要求用户输入两个数字,使用三元表达式求出两个数的最大值  添加try-catch
            try
            {
                Console.WriteLine("\n\n13.请输入第1个数字:");
                int n1 = int.Parse(Console.ReadLine());
                Console.WriteLine("请输入第2个数字:");
                int n2 = int.Parse(Console.ReadLine());
                int max = n1 > n2 ? n1 : n2;
                Console.WriteLine("两个数的最大值为{0}",max);
            }
            catch
            {
                Console.WriteLine("输入格式错误。");
            }
            #endregion

            #region 练习十四:要求用户输入性别,男--打印出,欢迎先生光临,女--打印出欢迎女士光临
            Console.WriteLine("\n\n14.请输入性别:(男/女)");
            string sex = Console.ReadLine();
            switch (sex)
            {
                case "男":
                    {
                        Console.WriteLine("欢迎先生光临!");
                        break;
                    }
                case "女":
                    {
                        Console.WriteLine("欢迎女士光临!");
                        break;
                    }
                default:
                    {
                        Console.WriteLine("输入错误");
                        break;
                    }

            }
            #endregion

            Console.ReadLine();
        }
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值