码迷,mamicode.com
首页 > Windows程序 > 详细

C# 《五》流程控制(1)

时间:2017-08-27 15:27:05      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:string   9.png   space   namespace   ...   分支语句   路径   不能   编程语言   

1、分支语句之 if 语句

1、流程控制语句是程序的核心部分,对任何一门编程语言来说都至关重要,是控制程序执行流向的基本语句。如果一门语言缺少了流程控制,就会缺少对程序流向的控制,就不能称之为计算机语言。

2、C#语言提供了丰富、灵活的控制流程语句,主要分分支语句、迭代语句、跳转语句三类。

分支语句为 if 语句与 switch 语句;能够根据实际情况决定逻辑路径代码。

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


3、对输入的数字进行判断

    > 10 提示大于 10

   < 10 提示小于 10

   = 10 提示等于 10

4、新语句

int Parse(Console.ReadLine());    //接受用户输入的整数  


程序如下:

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

namespace _5._1_分支语句_if_语句
{
    class Program
    {
        static void Main(string[] args)
        {
            //判断变量 a 中存放数值与 10 的关系
            Console.WriteLine("请输入数值,判断它与 10 的关系:");
            int a = int.Parse(Console.ReadLine());
            //int.Parse 用于将屏幕输入的语句转换为整型
            if(a < 10)
            {
                Console.WriteLine("a小于 10");
            }
            if(a == 10)
            {
                Console.WriteLine("a 等于 10");
            }
            if(a > 10)
            {
                Console.WriteLine("a 大于 10");
            }/*
            else   //如果前边的 if 语句一条也没有执行,那就执行 else 语句
                   //如果执行了其中一条 if 语句,那就不会执行else语句
            {
                Console.WriteLine("无法判断");
            }*/
            Console.ReadKey();
        }
    }
}

运行结果:
技术分享

 

2、分支语句之swirch 语句

实例:

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

程序如下:

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

namespace _5._2分支语句之switch语句
{
    class Program
    {
        static void Main(string[] args)
        {
            //输入 1 显示星期一,依次类推
            Console.WriteLine("请输入1-7中的一个数字");
            int week = int.Parse(Console.ReadLine());
            //if (week == 1) Console.WriteLine("星期一");
            //if (week == 2) Console.WriteLine("星期二");
            //if (week == 3) Console.WriteLine("星期三");
            //if (week == 4) Console.WriteLine("星期四");
            //if (week == 5) Console.WriteLine("星期五");
            //if (week == 6) Console.WriteLine("星期六");
            //if (week == 7) Console.WriteLine("星期天");

            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();
        }
    }
}

运行结果:
技术分享

2、判断2015年每个月份的天数

 程序如下:

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

namespace _5._2分支语句之switch语句
{
    class Program
    {
        static void Main(string[] args)
        {

            //判断2015年每个月的天数
            //31天:1、3、5、7、8、10、12
            //30天:4、6、9、11
            //28天:2
            Console.WriteLine("请输入月份数字");
            int month = int.Parse(Console.ReadLine());
            switch(month)
            {
                case 2: Console.WriteLine("本月有28天"); break;
                case 4: 
                case 6:
                case 9:
                case 11:
                    Console.WriteLine("本月有30天"); break;
                default: Console.WriteLine("本月有31天"); break;
            }
            Console.ReadKey();
        }
    }
}

运行结果:
技术分享

1、结构:

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

 

C# 《五》流程控制(1)

标签:string   9.png   space   namespace   ...   分支语句   路径   不能   编程语言   

原文地址:http://www.cnblogs.com/guijin/p/7440378.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!