C#学习笔记
第21题
namespace 第二十一题 { /*10. 编写程序在E盘下新建一文本文件,并对该文件 * 进行复制,移动,写入,读出操作*/ class Program { static void Main(string[] args) { //StreamWriter sw = new StreamWriter("E:\\test.txt", true, Encoding.GetEncoding("gb2312")); string[] data = { "hello world, how are you", "i'm pkm" }; //sw.WriteLine(data); //sw.Close(); File.WriteAllLines("E:\\test.txt", data, Encoding.GetEncoding("gb2312")); StreamReader sr = new StreamReader("E:\\test.txt", Encoding.GetEncoding("gb2312")); string line; while ((line = sr.ReadLine()) != null) { Console.WriteLine(line); } sr.Close(); if (File.Exists("E:\\test11.txt")) { File.Delete("E:\\test11.txt"); } File.Copy("E:\\test.txt", "E:\\test11.txt"); if (File.Exists("E:\\test22.txt")) { File.Delete("E:\\test22.txt"); } File.Move("E:\\test11.txt", "E:\\test22.txt"); Console.ReadKey(); } } }
第二十二题
namespace 二十二题 { /*11.写一个控制台应用程序,接收一个长度 * 大于3的字符串,完成下列功能: 1)输出字符串的长度。 2)输出字符串中第一个出现字母a的位置。 3)在字符串的第3个字符后面插入子串“hello”, * 输出新字符串。 4)将字符串“hello”替换为“me”,输出新字符串。 5)以字符“m”为分隔符,将字符串分离, * 并输出分离后的字符串。 */ class Program { static void Main(string[] args) { string ch; ch = Console.ReadLine(); //ch = "bastmaxthing"; Console.WriteLine("它的长度为{0}", ch.Length); Console.WriteLine("第一个出现a的位置是:{0}", ch.IndexOf('a') + 1); ch = ch.Insert(3, "hello"); Console.WriteLine("插入后的字符串:{0}", ch); ch = ch.Replace("hello", "me"); Console.WriteLine("hello替换成me后:{0}", ch); string[] strs = ch.Split('m'); Console.WriteLine("分割后得到的字符串:"); foreach (string str in strs) { Console.WriteLine("子串:{0}\n", str); } Console.ReadKey(); } } }
第二十三题
namespace di23 { /*分别用for,while,do…while语句编写程序 * ,实现求前n个自然数之和。 */ class Program { static void Main(string[] args) { double sum = 0; int i, num; Console.WriteLine("请你输入要计算的第N个整数:"); num = Convert.ToInt32(Console.ReadLine()); for (i = 0; i <= num; i++) { sum += i; } Console.WriteLine("你要的N个数的和为:{0}", sum); Console.ReadLine(); } /*//while循环 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { double sum = 0; int i=1, num; Console.WriteLine("请你输入要计算的第N个整数:"); num =Convert.ToInt32(Console.ReadLine()); while(i<=num) { sum += i; i++; } Console.WriteLine("你要的N个数的和为:{0}",sum); Console.ReadLine(); } } }*/ /*//do------while using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { double sum = 0; int i=1, num; Console.WriteLine("请你输入要计算的第N个整数:"); num =Convert.ToInt32(Console.ReadLine()); do { sum += i; i++; } while (i <= num); Console.WriteLine("你要的N个数的和为:{0}",sum); Console.ReadLine(); } } }*/ } }
第二十四题
namespace 第24题 { /*编程输出九九乘法表*/ class Program { static void Main(string[] args) { for (int i = 1; i < 10; i++) { for (int j = 1; j <= i; j++) { int k = i * j; Console.Write(i.ToString() + "*" + j.ToString() + "=" + k.ToString() + " "); } Console.WriteLine(); } Console.Read(); } } }
第二十五题
namespace 第25题 { /*编写一个程序,对输入的4个整数, * 求出其中最大值和最小值。 */ class Program { static void Main(string[] args) { int a, b, c, d, t; a = Convert.ToInt32(Console.ReadLine()); b = Convert.ToInt32(Console.ReadLine()); c = Convert.ToInt32(Console.ReadLine()); d = Convert.ToInt32(Console.ReadLine()); //判断最大数 t = a; if (b > t) t = b; if (c > t) t = c; if (d > t) t = d; Console.WriteLine("最大数是:{0}", t); //判断最小数 t = a; if (b < t) t = b; if (c < t) t = c; if (d < t) t = d; Console.WriteLine("最小数是:{0}", t); Console.ReadLine(); } } }
第二十六题
namespace 第26题 { class Program { static void Main(string[] args) { int n, sum = 0; Console.WriteLine("请输入数组的行列数:\n"); n = Convert.ToInt32(Console.ReadLine()); int[,] a = new int[n, n]; Random rnd = new Random(); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { a[i, j] = rnd.Next(100); if (i == j || (i + j) == (n - 1)) sum += a[i, j]; } } Console.WriteLine("随即初始化的数组是:\n"); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { Console.Write(" {0}", a[i, j]); } Console.WriteLine(); } Console.WriteLine("两条对角线上的元素值之和是: {0}", sum); Console.Read(); } } }
第二十七题
namespace 第27题 { /*用c#建立一个一维数组, * 使用该数组列出所学习的课程名称*/ class Program { static void Main(string[] args) { string[] subjects ={"学科1","学科2","学科3","学科4","学科5", "学科6","学科7","学科8","学科9","学科10"}; foreach(string s in subjects) { Console.WriteLine(s);//输出所有的学科 } } } }
第二十八题
namespace 第28题 { /*编写一个包含学生基本资料的结构类型数据(要求包括姓名,性别,年龄,身高,体重等)。*/ class Program { struct Student { public string Name; public string Sex; public int Age; public float Height; public float Weight; } static void Main(string[] args) { Student s = new Student(); s.Age = 25; s.Height = 178; s.Sex = "男"; s.Weight = 46; s.Name = "土豪"; Console.WriteLine("学生的姓名{0}身高{1}性别{2}重量{3}年龄{4}", s.Name ,s.Height ,s.Sex ,s.Weight ,s.Age ); Console.ReadLine(); } } }
浙公网安备 33010602011771号