visual C#(四)使用判断语句

参考书:《 visual C# 从入门到精通》
第一部分 Visual C# 和Visual Studio 2015概述
第4章 使用判断语句

4.1 声明布尔变量

4.2 使用布尔操作符

4.2.1 理解相等和关系操作符

==!=:相等 关系符、不等关系符

关系操作符:<<=>>=

4.2.2 理解条件逻辑操作符

AND(与)操作符&&,OR(或)操作符||

4.2.3 短路求值

&&的左操作数求值为false或者||的左操作数求值为true,这时将跳过对右侧布尔表达式的求值。

4.2.4 操作符的优先级和结合性总结

在这里插入图片描述

4.3 使用if语句做出判断

4.3.1 理解if 语句的语法

4.3.2 使用代码块分组语句

4.3.3 嵌套if语句

下面我们又是新建一个空白应用,来实现如下的功能:
在这里插入图片描述

窗体中显示两个DatePicker控件,显而易见的,这个程序就是比较两个如期的大小。需要注意的一个细节是TextBox控件中字符串要换行的话需要的指令是\r\n而不是\n,这个是需要注意的。其中的C#源代码如下:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x804 上介绍了“空白页”项模板

namespace c_4_3_3
{
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        private void compareClicked(object sender, RoutedEventArgs e)
        {
            int diff = dataCompare(firstDate.Date.LocalDateTime, secondDate.Date.LocalDateTime);
            showText.Text = " ";
            show("firstDate == secondDate", diff == 0);
            show("firstDate != secondDate", diff != 0);
            show("firstDate < secondDate", diff < 0);
            show("firstDate <= secondDate", diff <= 0);
            show("firstDate > secondDate", diff > 0);
            show("firstDate >= secondDate", diff >= 0);
        }
        private void show(string v1, bool v2)
        {
            showText.Text+=$"{v1}: {v2}\r\n ";
        }
        private int dataCompare(DateTime localDateTime1, DateTime localDateTime2)
        {
            int result;
            if (localDateTime1.Year < localDateTime2.Year)
                result = -1;
            else if (localDateTime1.Year > localDateTime2.Year)
                result = 1;
            else if (localDateTime1.Month < localDateTime2.Month)
                result = -1;
            else if (localDateTime1.Month > localDateTime2.Month)
                result = 1;
            else if (localDateTime1.Day < localDateTime2.Day)
                result = -1;
            else if (localDateTime1.Day > localDateTime2.Day)
                result = 1;
            else
                result = 0;
            return result;
        }
    }
}

4.4 使用switch语句

4.4.1 理解switch语句的语法

switch语句语法如下:

switch(controllingExpression){
    case constantExpression:
        statements;
        break;
        ...
   default:
        statements;
        break;
}

4.4.2 遵循switch语句的规则

switch语句需要遵循以下规则:

  • 控制表达式只能是某个整型(int,char,long等)或string
  • case标签必须是常量表达式
  • case标签必须唯一
  • 可以连续写多个case标签,中间不穿插额外的语句,这样最后一个标签后的代码使用所有的case。但如果两个标签之间以后额外的代码就不能从第一个标签贯穿到第二个标签,这样编译器会报错的。

下面我们又新建一个空白应用,我们要达到一个效果:赋值一段字符串,要求利用switch语句,将字符串中的XML特殊字符转换成对应的XML形式,如将字符串<换成字符串&lt;。效果如下图
在这里插入图片描述

C#源代码如下:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x804 上介绍了“空白页”项模板

namespace c_4_4_2
{
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        private void copyClick(object sender, RoutedEventArgs e)
        {
            for(int i=0;i<t1.Text.Length;++i)
            {
                //inRange=(lo<=number)&&(hi>=number);
                char current = t1.Text[i];
                switch (current)
                {
                    case '<':
                        t2.Text += "&lt;";
                        break;
                    case '>':
                        t2.Text += "&gt;";
                        break;
                    case '&':
                        t2.Text += "&amp;";
                        break;
                    case '\"':
                        t2.Text += "&#34";
                        break;
                    case '\'':
                        t2.Text += "&#39";
                        break;
                    default:
                        t2.Text += current;
                        break;
                }
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值