visual C#(五)使用复合赋值和循环语句

参考书:《 visual C# 从入门到精通》
第一部分 Visual C# 和Visual Studio 2015概述
第5章 使用复合赋值和循环语句

5.1 使用复合赋值操作符

*=

/=

%=

+=

-=

answer+=42,等效于answer=answer+42

5.2 使用while 语句

如:

int i=0;
while(i<10){
    Console.WriteLine(i);
}

下面我们尝试创建一个空白应用,实现一个简单的文本文件查看器的功能。这个项目还是有点难度的。书上都没有说明FileOpenPicker窗口的用法,所以我废了不少的劲去探索FileOpenPicker窗口的用法。需要注意一些细节,但由于我还处于入门阶段,现在是说不明白一些地方的所以然来。

  1. 首先是拖控件,一个Button,两个TextBox。布局如图就行了。在这里插入图片描述
  2. 添加Button控件的Click时间的处理方法openFileClick。同时注意在MainPage.xaml.cs源代码的using指令下添加:using Windows.Storage;//手动添加 using Windows.Storage.Pickers;//手动添加
  3. 我直接把代码放上来好了。注意openFileClick方法的返回类型的前面添加一个关键字async
  4. 开始调试,点击Open File,会出现一个文件选取对话框,找到我们本项目的C#源代码MainPage.xaml.cs点击“打开”即可。会出现下图的效果。
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;
using Windows.Storage;//手动添加
using Windows.Storage.Pickers;//手动添加
// https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x804 上介绍了“空白页”项模板

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

        private async void openFileClick(object sender, RoutedEventArgs e)
        {
            FileOpenPicker picker = new FileOpenPicker();
            picker.ViewMode = PickerViewMode.List;

            picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
            picker.FileTypeFilter.Add(".cs");

            StorageFile  file = await picker.PickSingleFileAsync();
            pathShow.Text = file.Path.ToString();
            var stringopenFile = await file.OpenAsync(FileAccessMode.Read);
            var inputStream = stringopenFile.GetInputStreamAt(0);
            TextReader reader = new StreamReader(inputStream.AsStreamForRead());
            displayData(reader);
        }

        private void displayData(TextReader reader)
        {
            sourceShow.Text = "";
            string line = reader.ReadLine();
            while (line != null)
            {
                sourceShow.Text += line + "\r\n";
                line = reader.ReadLine();
            }
            reader.Dispose();//释放与文件关联的资源并关闭文件
        }
    }
}

在这里插入图片描述

5.3 编写for语句

如下代码:

for(int i=0;i<10;++i){
    Console.WriteLine(i);
}

5.4 编写do语句

如下:

int i=0;
do
{
    Console.WriteLine(i);
    ++i;
}
while(i<10);

breakcontinuebreak跳出循环,continue结束当前循环立即开始下一次循环。

下面我又新建一个空白应用,展示将一个数字转换成八进制的步骤,效果如图:

在这里插入图片描述

源代码如下:

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

        private void stepsClick(object sender, RoutedEventArgs e)
        {
            int num = int.Parse(numberText.Text);
            stepsText.Text = "";
            string current = "";
            do
            {
                int nextDigit = num % 8;
                num /= 8;
                int digitCode = '0' + nextDigit;
                char digit = Convert.ToChar(digitCode);
                current = digit + current;
                stepsText.Text += current + "\r\n";
            } while (num != 0);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值