C#课堂笔记——第六周

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37813928/article/details/80000822

1.语句

;分号是表达一个语句结束的标志,单独一个分号也算是一个语句。

2.

else和相对最近的if相匹配

3.

switch语句中的变量不能是浮点数,因为switch语句进行判断是本质上是在做减法。

4.

C#每个case语句之后都必须有break等语句进行跳出,不能“贯穿”。

5.有穷自动机

6.foreach

foreach循环是一个特殊的for循环,只能读数据,不能写数据。

int[ ] a = {3, 17, 4, 8, 2, 29};
foreach (int x in a) sum += x;
string s = "Hello";
foreach (char ch in s) Console.WriteLine(ch);
Queue q = new Queue(); // elements are of type object
q.Enqueue("John"); q.Enqueue("Alice"); ...
foreach (string s in q) Console.WriteLine(s);

7.goto

C#允许goto语句,但是goto语句不能跳到{}块里面,也不能跳到finally块中。

8.

函数有返回值,过程没有返回值,一般不进行区分。

9.C#的输出

Console.Write("Hello {0}", name);
Console.WriteLine("{0} = {1}", x, y);

10.文件操作

using System;
using System.IO;
class Test {
static void Main() {
        FileStream s = new FileStream("output.txt",FileMode.Create);
        StreamWriter w = new StreamWriter(s);
        w.WriteLine("Table of sqares:");
        for (int i = 0; i < 10; i++)
        w.WriteLine("{0,3}: {1,5}", i, i*i);
        w.Close();
    }
}

同一个stream不能同时有多个StreamWriter进行操作。

//从一个文件中输入
using System;
using System.IO;
class Test {
static void Main() {
FileStream s = new FileStream("input.txt",FileMode.Open);
StreamReader r = new StreamReader(s);
string line = r.ReadLine();
while (line != null) {
        ...
        line = r.ReadLine();
        }    
        r.Close();
    }
}

猜你喜欢

转载自blog.csdn.net/qq_37813928/article/details/80000822