目录
3.有哪些算术运算符,有哪些关系运算符,有哪些逻辑运算符,有哪些位运算符,有哪些赋值运算符。
5.优先级口诀:有括号先括号,后乘除再加减,然后位移再关系,逻辑完后再条件。
6.写个例子展示break、continue、ruturn的区别。
1.实现通用打印泛型类,可以打印各个集合中的值,方便调试。
答:可以通过定义一个泛型类,并且提供一个用于打印集合元素的方法来实现,使用这个
Tools
类时,你可以传入任何类型的IEnumerable<T>
(如List、Array等)作为参数,它会遍历并打印出集合中的每个元素。class Tools//泛型类 { static public void println<T>(IEnumerable<T> collection)//泛型参数 { foreach (T item in collection)//变量泛型 { Console.Write(item+" "); } Console.WriteLine(); } }
例如,如果你有一个整数列表和一个字符串列表,你可以这样使用这个类:
HashSet<string> hs = new HashSet<string>(); hs.Add("王"); hs.Add("李"); hs.Add("张"); DJTools.println(hs);
2.计算遍历目录的耗时。int num = 1; int times = 1000 * 10000; //编译器优化,编译后直接使用计算结果 Stopwatch sw = new Stopwatch();//打印执行的时间,stopwatch sw.Start(); //计时开始 for (int i = 0; i < times; i++) { num<<=2; } sw.Stop(); //计时结束 Console.WriteLine("耗时:{0}毫秒", sw.ElapsedMilliseconds);
运行结果:
3.有哪些算术运算符,有哪些关系运算符,有哪些逻辑运算符,有哪些位运算符,有哪些赋值运算符。算术运算符:算术运算符用于执行基本的数学运算:
+
:加法-
:减法*
:乘法/
:除法(得到一个浮点数结果)%
:取模(求余数)++
:自增(前置或后置)--
:自减(前置或后置)关系运算符:关系运算符用于比较两个值,返回布尔值(
true
或false
):
==
:等于!=
:不等于<
:小于>
:大于<=
:小于等于>=
:大于等于逻辑运算符:逻辑运算符用于组合布尔表达式:
&&
:逻辑与(短路与,如果左边为false
,右边不计算)||
:逻辑或(短路或,如果左边为true
,右边不计算)!
:逻辑非位运算符:位运算符用于对整数类型数据的二进制位进行操作:
&
:按位与|
:按位或^
:按位异或~
:按位取反(一元运算符)<<
:左移>>
:右移赋值运算符:赋值运算符用于给变量分配值:
=
:简单赋值+=
:加法赋值-=
:减法赋值*=
:乘法赋值/=
:除法赋值%=
:取模赋值&=
:按位与赋值|=
: 按位或赋值^=
:按位异或赋值<<=
:左移赋值>>=
:右移赋值
4.三目表达式举例。
答:C#中的三目运算符(也称条件运算符)是一种简洁的条件表达式方式,其语法结构为:条件 ? 表达式1 : 表达式2。如果条件为真(true),则计算并返回表达式1的值;如果条件为假(false),则计算并返回表达式2的值。
举例1
int a = 10; int b = 20; string result = a > b ? "a 大于 b" : "a 小于 b"; Console.WriteLine(result);
运行结果:
举例2:嵌套使用
int score = 85; string grade = score >= 90 ? "A" : score >= 80 ? "B" : "C"; Console.WriteLine(grade);
运行结果:
举例3:字符串操作:当输入的为空时执行表达式1,不为空时执行表达式2。
while (true) { string name = Console.ReadLine(); if (name == "quit" || name == "exit") {break;} string greeting = string.IsNullOrEmpty(name) ? "你好,匿名用户!" : $"你好,{name}!"; Console.WriteLine(greeting); }
运行结果:
5.优先级口诀:有括号先括号,后乘除再加减,然后位移再关系,逻辑完后再条件。举例:
int a = 2; int b = 3; int c = 4; int d = 5; int result; result = a+(a + b) * c / d;//有括号先括号,后乘除再加减 Console.WriteLine(result); int e = a << 6; bool f = e > 200;//位移再关系 Console.WriteLine("左移6位后为 {0},并与200比较的结果是 {1}。",e,f); bool x = true; bool y = false; bool z = x && y; string o =z? "满足条件" : "不满足条件";//逻辑完后再条件 Console.WriteLine(o);
运行结果:
6.写个例子展示break、continue、ruturn的区别。break的作用主要是跳出循环,不再执行循环。
continue的作用主要是跳过本次循环,执行下一次循环。
using System; class Program { static void Main(string[] args) { Console.WriteLine("break"); for (int i = 0; i < 10; i++) { if (i == 5) { Console.WriteLine(i); break; // 遇到5时,跳出循环 } Console.WriteLine(i); } Console.WriteLine("continue"); for (int j = 0; j < 10; j++) { if (j % 2 == 0) { continue; // 跳过偶数,不打印 } Console.WriteLine(j); // 只打印奇数 } } }
运行结果:
return:当执行流遇到
return
语句时,当前方法的执行会被立即终止,控制权返回给调用该方法的地方。
7.写个冒泡排序。答:冒泡排序是一种基础的排序算法,通过重复遍历要排序的数列,比较每对相邻元素的值,如果顺序错误就把它们交换过来。遍历数列的工作重复进行,直到没有再需要交换的元素为止,这时数列就已经排序完成了。
int[] arr = { 6,4,5,1,2,7,9,8,3 }; static void Write(int[] arr) { foreach (int item in arr) { Console.Write(item + " "); } Console.WriteLine(); } Console.WriteLine("排序前:"); Write(arr); static void Sort(int[] arr) { int n = arr.Length; for (int i = 0; i < n - 1; i++) { int x = 0; for (int j = 0; j < n - i - 1; j++) { // 如果当前元素比下一个元素大,则交换它们的位置 if (arr[j] > arr[j + 1]) { x++; int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } Console.WriteLine("移动{0}次",x); } Console.WriteLine(); } Console.WriteLine("排序后:"); Sort(arr); Write(arr);
运行结果:
8.写个九九乘法表。答:九九乘法表可以通过嵌套循环实现。
for (int i = 1; i <= 9; i++) { for (int j = 1; j <= i; j++) { Console.Write($"{j}x{i}={i * j}"+" "); } Console.WriteLine(); }
运行结果:
9.实现文件找不到抛出异常。using System; using System.IO; class Program { static void Found(string filePath) { if (!File.Exists(filePath)) { throw new FileNotFoundException("文件不存在"); } } static void Main(string[] args) { string filePath = "C:\\DumpStack8.log"; try { Found(filePath); Console.WriteLine("文件存在"); } catch (FileNotFoundException) { Console.WriteLine("文件未找到异常"); } catch (Exception) { Console.WriteLine("其他异常"); } } }
运行结果: