找到 34423 篇文章 相关编程

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

Arjun Thakur
更新于 2020年6月22日 09:55:47

166 次查看

要查找平均值,首先设置一个数组 int[] myArr = new int[10] {    45,    23,    55,    15,    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() {       int[] myArr = new int[10] {          45,          23,          55,          15,          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;       Console.WriteLine("Sum = " + sum);       Console.WriteLine("Average Of integer elements = " + average);    } }

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

Samual Sam
更新于 2020年6月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年6月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("Initial string = " + str);       string res = str.Replace('*', 'o');       // 替换后       Console.WriteLine("After replacing asterisk = " + res.ToString());    } } 输出 Initial string = dem* text After replacing asterisk = demo text

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

karthikeya Boyini
更新于 2020年6月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("Initial string = " + str);       string res = str.Replace('$', 'k');       // 替换后       Console.WriteLine("Replaced string = " + res.ToString());    } } 输出 Initial string = abcd$ef$gh Replaced string = abcdkefkgh

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

George John
更新于 2020年6月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("After removing duplicate words..."); ... 阅读更多

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

Chandu yadav
更新于 2020年6月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("Initial Set...");       foreach(var val in names) {          Console.WriteLine(val);       }       names.RemoveWhere(x => x ... 阅读更多

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

Samual Sam
更新于 2020年6月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("Initial List..."); ... 阅读更多

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

Arjun Thakur
更新于 2020年6月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年6月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("Initial List...");       foreach(int a in list) ... 阅读更多

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

Samual Sam
更新于 2020年6月22日 09:49:07

752 次查看

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

广告
© . All rights reserved.