C#流程控制

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

C#流程控制

Mwyldnje2003   2022-09-28 我要评论

流程控制语句分类

  • 分支语句: if语句和switch语句
  • 迭代语句
  • 跳转语句

1、if语句

if (判断条件表达式){ 表达式结果为true时执行}else{表达式结果为false时执行}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace if语句
{
    class Program
    {
        static void Main(string[] args)
        {
            //判断a变量与10的关系
            Console.WriteLine("请输入你要比较的第一个数字");
            int a=Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("请输入你要比较的第而个数字");
            //int.parse 用于将屏幕输入的语句转换为整型
            int b = int.Parse(Console.ReadLine());
            if (a < b)
            {
                Console.WriteLine("您输入的第一个数字{0}小于第二个数字{1}", a,b);
            }
            else if (a == b)
            {
                Console.WriteLine("您输入的第一个数字{0}等于第二个数字{1}", a,b);
            }
            else {
                Console.WriteLine("您输入的第一个数字{0}大于第二个数字{1}", a,b);
            }
            Console.ReadKey();
        }
    }
}

2、switch

输入1显示为星期一,依次类推

swithc(条件表达式){
case 常量表达式:条件语句;
case 常量表达式:条件语句;
case 常量表达式:条件语句;
default:条件表达式
}

控件无法从最终用例标签(XX)脱离开关——程序无法判定为结束,所以必须加一个break;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace switch控制语句
{
    class Program
    {
        static void Main(string[] args)
        {
            // 输入一显示星期一,一次类推
            Console.WriteLine("请输入1-7的数字");
            int week = int.Parse(Console.ReadLine());
            switch (week) {
                case 1: Console.WriteLine("星期一"); break;  //结束当前代码体
                case 2: Console.WriteLine("星期二"); break;
                case 3: Console.WriteLine("星期三"); break;
                case 4: Console.WriteLine("星期四"); break;
                case 5: Console.WriteLine("星期五"); break;
                case 6: Console.WriteLine("星期六"); break;
                case 7: Console.WriteLine("星期日"); break;
                default: Console.WriteLine("您输入的数据错误"); break; //超出规定值设置相应提示
            }
            Console.ReadKey();


            //判断2020年每个月的天数, 1,3,5,7,8,10,12为31天,4,6,9,11位30天,二月29天
            Console.WriteLine("请输月份数");
            int month = int.Parse(Console.ReadLine());
            switch (month)
            {
                
                case 2: Console.WriteLine("您输入的{0}月份有28天",month); break;                
                case 4:               
                case 6:
                case 9:
                case 11:
                    Console.WriteLine("您输入的{0}月份有30天",month); break;
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    Console.WriteLine("您输入的{0}月份有31天", month); break;
                default: Console.WriteLine("您输入的{0}月份错误", month); break; 
            }
            Console.ReadKey();
        }
    }
}

3、三位运算符

条件判断表达式?成立是执行的语句:不成立时执行的语句
三元运算符适用条件:只使用与判断具有两个结果的情况

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 三位运算符
{
    class Program
    {
        static void Main(string[] args)
        {
            // 判断输入述职与10的关系(<10 提示小于10, >=10提示大于等于10)
            Console.WriteLine("请输入您要比较的数据");
            int number = int.Parse(Console.ReadLine());
            //Console.WriteLine(number < 10 ? Console.WriteLine("小于10") : Console.WriteLine("大于等于10") );
            Console.WriteLine(number < 10 ? "小于10" : "大于等于10");
            Console.ReadKey();
        }
    }
}

4、迭代语句之while语句

4.1 迭代语句概述

迭代语句时程序中重复的执行,直到满足指定以条件才停止的一段代码。当用户想重复执行某些语句时,可依据当前不同的任务,

选择不同的循环依据使用,分别是:

  • while语句
  • do……while语句
  • for语句
  • foreach语句

4.2 while语句

while(条件表达式){
代码语句
}
while语句当条件满足时才执行

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

namespace while语句
{
    class Program
    {
        static void Main(string[] args)
        {
            //输出1-50的数字到屏幕上
            int a = 1;
            while (a<=50){

                Console.WriteLine(a);
                a++;
               
            }
            Console.ReadKey();
        }
    }
}

5、迭代语句之do……while

do{
循环体语句
}while();
do……while语句至少执行一次,即使条件不成立也会执行一次

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace do__while
{
    class Program
    {
        static void Main(string[] args)
        {
            //输出1-50的数字到屏幕上
            int num = 0;
            do {
                num++;
                Console.WriteLine(num);

            } while (num < 50);
            // 计算现金存入银行多长时间才可以答案到我们的预期收益(均按一年期定期存款,到期后自动转存)
            // 分析题目需要的变量  :本金, 目标收益,利率  时间(年)
            // 一年的收益: 本金*(1+利率)*1年 
            double Balace = 0;
            double Rate = 0;
            int Year = 0;
            double TargetBalace = 0;
            Console.WriteLine("请输入您的本金");
            Balace = double.Parse(Console.ReadLine());
            Console.WriteLine("请输入您的当前利率百分比");
            Rate = double.Parse(Console.ReadLine())/100;
            Console.WriteLine("请输入您的目标收益");
            do {
                TargetBalace = double.Parse(Console.ReadLine());
                if (TargetBalace<Balace) {
                    Console.WriteLine("恭喜您现在已经拥有了{0}元,请输入一个更大的目标",TargetBalace);
                }
            } while (TargetBalace<Balace);
            do
            {
                Balace *= (Rate + 1);
                Year++;
            } while (Balace < TargetBalace);
            Console.WriteLine("您将在{0}年内,获得{1}元的收益",Year,Balace);
            Console.ReadKey();           
        }
    }
}

6、迭代语句之for循环语句

for循环可以循环次数的限定,并维护自己的计时器;
有时候我们会省略初始条件,判断条件,循环条件,但两个分号不能省略

for(初始条件;判断条件;循环条件){
循环语句
}

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

namespace for循环语句
{
    class Program
    {
        static void Main(string[] args)
        {
            //求输入数据的阶乘
            // 1!=1  2!=2x1;  3!=3x2x1
            Console.WriteLine("请输入你要计算的阶乘数");

            for (;;) {
            int num = int.Parse(Console.ReadLine());
            int result = 1;
            for (int i=num; i!=0; i--) {
                result *= i;

            };
            Console.WriteLine("{0}的阶乘结果是{1}", num, result);
            };
            //Console.ReadKey();

        }
    }
}

for循环嵌套(九九乘法表)
循环嵌套就是一个循环中嵌套着另一个循环
使用for循环时,一般在for循环语句进行声明循环计数次的变量

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

namespace for循环语句
{
    class Program
    {
        static void Main(string[] args)
        {
            //九九乘法表
            Console.WriteLine("==================九九乘法口诀=========================");
            for (int i = 1; i < 10; i++) {
                for (int j=1; j<= i; j++) {
                    Console.Write("{0}X{1}={2}\t", j, i, j * i);
                }
                Console.WriteLine();
            }
            Console.ReadKey();


        }
    }
}

7、迭代语句之foreach

foreach提供了一个for语句的捷径,而且还存进了集合类更为一致

foreach(类型;变量;in 集合){
代码体
}

string类型(字符串)可以看成是char类型(字符)的一个集合
char.IsWhiteSpace© 判断字符是不是空格
foreach每执行一内含代码,循环变量就会一次读取集合中的一个元素,向当时循环便利
此处循环变量只是一个只读型的局部变量,这个值如果被修改编译会报错

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace @foreach
{
    class Program
    {
        static void Main(string[] args)
        {
            //将语句识别为单词,并逐行输出
            //语句用string类型,字母用char
            Console.WriteLine("请输入一句英文语句");
            string sentence = Console.ReadLine();
            foreach (char word in sentence)
            {
                if (char.IsWhiteSpace(word))
                {
                    Console.WriteLine();
                }
                else
                {
                    Console.Write(word);
                    //word='t';  //foreach语句的迭代变量不允许重新赋值
                }
            }
            Console.ReadLine();

        }
    }
}

8、跳转语句之break语句

跳转语句是程序运行到某一位置时,可以跳转到程序中另一行代码的语句

  • break:1)switch语句中用于从case语句中跳出,结束switch分支语句。2)用于跳出迭代语句结束当前训话
  • continute语句
  • goto语句
  • return语句

通过迭代语句,准备输出1~500这500个数,每行输出10个数。当输出的值同时是2、3、4、5、6】7的倍数是,跳出for迭代语句。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace break语句
{
    class Program
    {
        static void Main(string[] args)
        {
            //通过迭代语句,准备输出1~500这500个数,每行输出10个数。当输出的值同时是2、3、4、5、6、7的倍数是,跳出for迭代语句。
            Console.WriteLine("输出1~500这500个数,每行输出10个数");
            for (int i=1;i<501;i++) {
                if (i % 2 == 0 && i % 3 == 0 && i % 4 == 0 && i % 5 == 0 && i % 6 == 0 && i % 7 == 0) {
                    Console.WriteLine();
                    Console.WriteLine("2、3、4、5、6、7的最小公倍数倍数是"+i);

                    break;
                }
                if (i % 10 == 0)
                {
                    Console.WriteLine(i);
                }
                else Console.Write(i + "\t");
            }
            Console.ReadKey();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace break语句
{
    class Program
    {
        static void Main(string[] args)
        {
            //通过迭代语句,准备输出1~500这500个数,每行输出10个数。当输出的值同时是2、3、4、5、6、7的倍数是,跳出for迭代语句。
            Console.WriteLine("输出1~500这500个数,每行输出10个数");
            for (int i=1;i<501;i++) {
                if (i % 2 == 0 && i % 3 == 0 && i % 4 == 0 && i % 5 == 0 && i % 6 == 0 && i % 7 == 0) break;
                //{
                //    Console.WriteLine();
                //    Console.WriteLine("2、3、4、5、6、7的最小公倍数倍数是"+i);

                //    break;
                //}
                if (i % 10 == 0)
                {
                    Console.WriteLine(i);
                }
                else Console.Write(i + "\t");
            }
            Console.ReadKey();
        }
    }
}

9、continue语句

用于停止当前的迭代语句,结束本次循环,进入下一次循环(本次循环中continue后面的语句不执行)。breack是直接结束循环
只能用于迭代语句中

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

namespace continute语句
{
    class Program
    {
        static void Main(string[] args)
        {
            //实现50以内的奇数输出,利用continue
            Console.WriteLine("请输入一个数,会自动显示小于次数的所有奇数");
            int num = int.Parse(Console.ReadLine());
            for (int i = 1; i < num+1; i++)
            {
                if (i % 2 == 0) continue;  //满足条件时跳出此次循环,进入下一个循环;且本次循环continute后的语句不执行
                Console.WriteLine(i);
               
                
            }
            Console.ReadLine();
        }
    }
}

10、跳转语句之return

return语句使用时,一般有两种格式:1)return; 2)return 表达式;
return语句只能出现在方法当中,当调佣方法时,执行到return语句时;直接跳转到main()函数

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

namespace continute语句
{
    class Program
    {
        static void Main(string[] args)
        {
            //实现50以内的奇数输出,利用continue
            Console.WriteLine("请输入一个数,会自动显示小于次数的所有奇数");
            int num = int.Parse(Console.ReadLine());
            for (int i = 1; i < num+1; i++)
            {
                if (i % 2 == 0) continue;  //满足条件时跳出此次循环,进入下一个循环;且本次循环continute后的语句不执行
                Console.WriteLine(i);
               
                
            }
            Console.ReadLine();
        }
    }
}

使用方法实现:

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

namespace @return
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入三个整数,按回车键确认每个数的输入");
            int a = int.Parse(Console.ReadLine());
            int b = int.Parse(Console.ReadLine());
            int c = int.Parse(Console.ReadLine());
            //double averageresult = (a + b + c) / 3;
            double averageresult = average(a,b,c);
            Console.WriteLine("您输入的三个数{0}、{1}、{2}的平均数是{3}",a,b,c, averageresult);
            Console.ReadKey();
        }

        static double average(int a, int b, int c) {
            return (a + b + c) / 3;
        }
    }
}

11、跳转语句之goto

格式:goto 标标识符;
标识符标识程序位置的方法
标识方法——标识符+“:”

作用:当程序执行到goto语句时,程序会直接跳转到标识符所表示的程序位置。继续执行
goto的使用会使代码的易读性下降,在编写程序的时候尽量少用goto语句

任务:利用goto语句实现选择题:

5!=?
1、5!=5
2、5!=10
3、5!=20
4、5!=60

如果选择真确,提示:恭喜你,答对了!
如果选择错误,提示:很遗憾,你答错了
如果选择的选项不是1、2、3、4,提示:你所选的选项不存在

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace goto语句
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 0;
            Console.WriteLine("请选择5的阶乘正确答案,输入选项编号回车键确认");
            Console.WriteLine("1. 5!=5\n2. 5!=10\n3. 5!=20\n4. 5!=60\n");

        error:
            {
                a++;  //第一次执行时 a=1;因此不执行,当goto跳转到此语句时,再次自加1,a=2此时执行下面语句
                if (a > 1) Console.WriteLine("很遗憾,您打错了,请重新输入答案");  // 加入a判断条件原因是,避免在第一次执行是输出此提示
            }
        input:  int result = int.Parse(Console.ReadLine());
            
            switch (result) {
                case 1:
                case 2:
                case 3: goto error;
                case 4: goto right;
                default:
                    Console.WriteLine("您的选项{0}不存在,请重新输入",result);
                    goto input;
            }
        right:
            {
                Console.WriteLine("恭喜你答对了!");
            }
            Console.ReadKey();

        }
    }
}

12、任务实施

接受a\b\c三个整数,然后输出三个数中居中的那个数,并输出其阶乘

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 任务实施
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入三个整数");
            int a = Convert.ToInt32(Console.ReadLine());
            int b = Convert.ToInt32(Console.ReadLine());
            int c = Convert.ToInt32(Console.ReadLine());
            //判断中间变量
            ///如果a是中间值,那么有两种情况,b是最大值或b是最小值
            int temp = 0;
            int jc = 1;
            if ((a>=b && a<=c) || (a>=c && a<=b)) {
                Console.WriteLine(a + "是中间值");
                temp = a;
                Console.WriteLine("错误");
            }
            if (b >= a && b <= c || b >= c && b <= a)
            {
                Console.WriteLine(b + "是中间值");
                temp = b;
            }
            if (c >= a && c <= b || c >= b && c <= a)
            {
                Console.WriteLine(c + "是中间值");
                temp = c;
            }
            for (int i = 1; i < b+1; i++)
            {
                jc *= i;
            }
            Console.WriteLine("中间数{0}阶乘结果是{1}",temp,jc);
            Console.ReadKey();
        }
    }
}

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们