{
//将传递的字符串对应的索引位上的值转换成数字。
index = Convert.ToInt32(str[strCount].ToString()); //当月份的第二位为0时什么都不用做否则找到汉字数组中对应的汉字 if (index != 0) {
sb.Append(chineseWord[index]); } } }
return sb.ToString(); }
3、参考答案
public static void Main(string[] args) {
string str = \一日,天气炎热。两香蕉在路上散步,一香蕉抱怨道:“好热,好热啊。”遂脱衣。
另一香蕉遂倒\
StringBuilder sb = new StringBuilder(); if (str.Contains(\脱衣\ {
int index = str.IndexOf(\脱衣\ string str1 = str.Substring(0,index); string str2 = str.Substring(index + 2); sb.Append(str1); sb.Append(\
sb.Append(str2); }
Console.WriteLine(sb.ToString()); Console.ReadKey(); }
第8章 文件操作
一、填空题
1、输入流、输出流 2、Copy() 3、GetParent() 4、System.IO
5、Directory、DirectoryInfo 6、[Serializable] 7、GetFullPath() 8、序列化 9、FileMode
10、FileStream 二、判断题
1、错 2、对 3、对 4、错 5、错 三、选择题
1、A 2、C 3、ABD 4、C 5、C 6、AB 7、A 8、D 9、C 10、A 四、程序填空题
1、File.Exists(@\、@\、File.Copy、File.Delete 2、sr.ReadLine()、sw.Dispose() 五、简答题
1、流是对计算机输入输出设备之间数据传输的一种抽象,例如通过键盘输入数据、显示器显示程序的运行结果等这些都是流。
2、StreamReader和StreamWriter类都是以用于处理文本文件的,只是StreamReader用于读取文件,StreamWriter用于写入文件。
3、File类和FileInfo类都可以对文件进行创建、移动、查询和删除等操作,不同的是File类是一个静态类,而FileInfo是一个实例类,它的所有方法必须通过实例化对象后才能调用。
六、编程题
1、参考答案
class Program {
static void Main(string[] args) {
string source = \ string target = \
//创建文件流
using (FileStream fsRead = new FileStream(source, FileMode.Open)) {
using (FileStream fsWrite = new FileStream(target, FileMode.Create)) {
//创建写入流 //缓冲区
byte[] bytes = new byte[1024]; //循环读取文件流 while (true) {
int r = fsRead.Read(bytes, 0, bytes.Length); if (r <= 0) {
break; }
//写入文件
fsWrite.Write(bytes, 0, bytes.Length); }
} }
Console.WriteLine(\文件source的内容已写入文件target中\ Console.ReadKey(); }
2、 参考答案
class Program {
static void Main(string[] args) {
//读取文件中的所有行
string[] strAllLines = File.ReadAllLines(@\ bool b = false; //允许输入4次错误密码
for (int count = 1; count <= 5; count++) {
Console.WriteLine(\第{0}次输入用户名:\ string name = Console.ReadLine();
Console.WriteLine(\第{0}次输入密码:\ string pwd = Console.ReadLine(); //循环遍历文件中存储的用户名和密码进行匹配
for (int line = 0; line < strAllLines.Length; line++) {
//将文件中每一行中的用户名和密码分割开(用户名:密码)
string[] linstr = strAllLines[line].Split(new char[] { ':' },
StringSplitOptions.RemoveEmptyEntries);
//匹配成功调处当前内层循环
if (name == linstr[0] && pwd == linstr[1]) {
b = true; break; } }
//登录成功跳出外层循环 if (b) {
break; } else {
if(count<5) {
Console.WriteLine(\第{0}次输入密码错误,请重新输入\ }
else {
Console.WriteLine(\密码输入错误,游戏结束\ } } }
第9章 集合
一、填空题
1、 集合
2、 AddRange() 3、Clear()
4、 ArrayList Hashtable 5、 个数 6、 Count 7、 Sort() 8、 键 值 9、 Add()
10、Max() Min() 二、判断题
1、错 2、对 3、对 4、错 5、对 三、选择题
1、D2、B3、BD4、B5、AD6、BD7、A8、C9、CD10、D 四、程序分析题
1、编译不通过。泛型集合list在初始化时已经指定类型占位符T为string类型,也就是list集合只能添加string类型的元素。本题中,list集合加入了一个int类型的元素,所以编译不通过。 2、输出结果为: 1:张三 2:李四 3:王五
3、编译不通过。集合hash是键值对类型集合,键值对集合没有Sort方法,所以编译不通过。 五、简答题
1、参考答案 相同点:
1、 ArrayList集合与List
2、 ArrayList集合与List
遍历。
不同点:
1、 ArrayList可以存储任意多种类型元素。List
内部元素进行求和。Max()方法可以求得List