找到 2628 篇文章 关于 C#

如何在 C# 中查找整数数组元素的平均值?

Samual Sam
更新于 2020-06-22 09:56:58

168 次浏览

以下是我们的整数数组 - int[] myArr = new int[6] {    8,    4,    2,    5,    9,    14 };首先,获取数组的长度,并循环遍历数组以查找元素的总和。之后,将其除以长度。int len = myArr.Length; int sum = 0; int average = 0; for (int i = 0; i < len; i++) {    sum += myArr[i]; } average = sum / len;以下是完整代码示例 在线演示using System; public class Program {    public static void Main() {       ... 阅读更多

C# 程序,用星号替换句子中的字符

Ankith Reddy
更新于 2020-06-22 09:57:18

1K+ 次浏览

使用 Replace() 方法用星号替换字符。假设我们的字符串为 - string str = "dem* text";要替换它,请使用 Replace() 方法 - str.Replace('*', 'o');以下是完整代码 - 示例 在线演示using System; public class Program {    public static void Main() {       string str = "dem* text";       Console.WriteLine("初始字符串 = " + str);       string res = str.Replace('*', 'o');       // 替换后       Console.WriteLine("替换星号后 = " + res.ToString());    } }输出初始字符串 = dem* text 替换星号后 = demo text

C# 程序,从字符串中替换特殊字符

karthikeya Boyini
更新于 2020-06-22 09:57:38

5K+ 次浏览

假设我们的字符串为 - string str = "abcd$ef$gh";要替换特殊字符,请使用 Replace() 方法。string res = str.Replace('$', 'k');以下是替换字符串中字符的完整代码 - 示例 在线演示using System; public class Program {    public static void Main() {       string str = "abcd$ef$gh";       Console.WriteLine("初始字符串 = " + str);       string res = str.Replace('$', 'k');       // 替换后       Console.WriteLine("替换后的字符串 = " + res.ToString());    } }输出初始字符串 = abcd$ef$gh 替换后的字符串 = abcdkefkgh

C# 程序,从给定句子中删除所有重复的单词

George John
更新于 2020-06-22 09:44:04

1K+ 次浏览

设置一个包含重复单词的字符串。string str = "One Two Three One";上面,您可以看到单词“One”出现了两次。要删除重复的单词,您可以尝试在 C# 中运行以下代码 - 示例 在线演示using System; using System.Linq; public class Program {    public static void Main() {       string str = "One Two Three One";       string[] arr = str.Split(' ');       Console.WriteLine(str);       var a =       from k in arr       orderby k       select k;       Console.WriteLine("删除重复单词后..."); ... 阅读更多

C# 程序,从集合中删除项目

Chandu yadav
更新于 2020-06-22 09:45:15

194 次浏览

首先,声明一个 HashSet 并添加元素 - var names = new HashSet(); names.Add("Tim"); names.Add("John"); names.Add("Tom"); names.Add("Kevin");要删除元素,请使用 RemoveWhere。names.RemoveWhere(x => x == "John");让我们看看完整的示例 - 示例using System; using System.Collections.Generic; public class Program {    public static void Main() {       var names = new HashSet < string > ();       names.Add("Tim");       names.Add("John");       names.Add("Tom");       names.Add("Kevin");       Console.WriteLine("初始集合...");       foreach(var val in names) {          Console.WriteLine(val);       }       names.RemoveWhere(x => x ... 阅读更多

C# 程序,打印列表中的唯一值

Samual Sam
更新于 2020-06-22 09:44:45

6K+ 次浏览

设置列表。List < int > list = new List < int > (); list.Add(99); list.Add(49); list.Add(32);获取唯一元素。List myList = list.Distinct().ToList();以下是显示列表中唯一值的完整示例。示例 在线演示using System; using System.Collections.Generic; using System.Linq; public class Demo {    public static void Main() {       List < int > list = new List < int > ();       list.Add(55);       list.Add(45);       list.Add(55);       list.Add(65);       list.Add(73);       list.Add(24);       list.Add(65);       Console.WriteLine("初始列表..."); ... 阅读更多

C# 程序,检查列表中所有值是否大于给定值

Arjun Thakur
更新于 2020-06-22 09:47:12

1K+ 次浏览

假设您需要查找以下列表中的元素是否大于 80。int[] arr = new int[] {55, 100, 87, 45};为此,循环遍历直到数组长度。这里,res = 80,即给定的元素。for (int i = 0; i < arr.Length; i++) {    if(arr[i] res) {                Console.WriteLine(arr[i]);             }          }       }    } }

C# 程序,从列表中删除重复元素

karthikeya Boyini
更新于 2020-06-22 09:46:18

538 次浏览

声明一个列表并添加元素。List list = new List(); list.Add(50); list.Add(90); list.Add(50); list.Add(100);现在,使用 Distinct() 方法仅获取唯一元素。List myList = list.Distinct().ToList();以下是从列表中删除重复元素的完整代码 - 示例 在线演示using System; using System.Collections.Generic; using System.Linq; public class Demo {    public static void Main() {       List < int > list = new List < int > ();       list.Add(50);       list.Add(90);       list.Add(50);       list.Add(100);       Console.WriteLine("初始列表...");       foreach(int a in list) ... 阅读更多

C# 程序,检查两个列表是否至少有一个公共元素

Samual Sam
更新于 2020-06-22 09:49:07

752 次浏览

设置第一个列表。int[] arr1 = {    65,    57,    63,    98 };现在,设置第二个列表。int[] arr2 = {    43,    65,    33,    57 };现在让我们看看使用 == 和 < 运算符检查两个列表是否具有公共元素的完整代码。示例 在线演示using System; using System.Collections.Generic; using System.Linq; public class Program {    public static void Main() {       int[] arr1 = {          65,          57,          63,          98       }; ... 阅读更多

如何使用属性查找锯齿数组的长度?

Ankith Reddy
更新于 2020-06-22 09:49:50

1K+ 次浏览

首先,声明并初始化一个锯齿数组。int[][] arr = new int[][] { new int[] {    0,    0 }, new int[] {    1,    2 }, new int[] {    2,    4 }, new int[] {    3,    6 }, new int[] {    4,    8 } };现在使用 length 属性获取长度。arr.Length以下是完整代码 - 示例 在线演示using System; public class Program {    public static void Main() {       int[][] arr = new int[][]{new int[]{0,0},new int[]{1,2}, new int[]{2,4},new int[]{ 3, 6 }, new       int[]{ 4, 8 } };       // 长度       Console.WriteLine("长度:"+arr.Length);       Console.WriteLine("上限: {0}",arr.GetUpperBound(0).ToString());       Console.WriteLine("下限: {0}",arr.GetLowerBound(0).ToString());       Console.WriteLine("数组维度: " + arr.Rank);    } }输出长度:5 上限: 4 下限: 0 数组维度: 1

广告

© . All rights reserved.