C#课堂笔记——第六周

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值