找到 34423 篇文章 编程

如何在 C# 中将十六进制字符串转换为十六进制数字?

karthikeya Boyini
更新于 2020-06-22 13:04:03

2K+ 次浏览

首先,设置十六进制字符串 - string str = "7D"; 现在,使用 Convert.ToSByte() 方法将十六进制字符串转换为十六进制数字 - Console.WriteLine(Convert.ToSByte(str, 16)); 让我们看看完整的代码 - 示例实时演示 using System; namespace Demo {    public class Program {       public static void Main(string[] args) {          string str = "7D";          Console.WriteLine(Convert.ToSByte(str, 16));       }    } } 输出 125 另一种将十六进制字符串转换为十六进制数字的方法 - 示例实时演示 using System; namespace Demo {    public class Program {       public static void Main(string[] args) {          string str = "7D";          Console.WriteLine(Convert.ToInt32(str, 16));       }    } } 输出 125

如何在 C# 中将元组转换为数组?

Samual Sam
更新于 2020-06-22 12:53:45

836 次浏览

首先,设置一个元组 - Tuple t = Tuple.Create(99,53); 现在,将元组转换为数组 - int[] arr = new int[]{t.Item1, t.Item2}; 以下是将元组转换为数组的代码 - 示例实时演示 using System; using System.Linq; using System.Collections.Generic; namespace Demo {    public class Program {       public static void Main(string[] args) {          Tuple t = Tuple.Create(99,53);          int[] arr = new int[]{t.Item1, t.Item2};          foreach (int val in arr) {             Console.WriteLine(val);          }       }    }   } 输出 99 53

如何在 C# 中使用递归将数字从十进制转换为二进制?

karthikeya Boyini
更新于 2020-06-22 12:54:49

393 次浏览

要获取十进制的二进制,使用递归,首先设置十进制数字 - int dec = 30; 现在将值传递给一个函数 - public int displayBinary(int dec) { } 现在,检查条件,直到十进制值为 0,并使用递归获取十进制数字的模 2,如下所示。 递归调用将再次调用该函数,并使用 dec/2 值 - public int displayBinary(int dec) {    int res;    if (dec != 0) {       res = (dec % 2) + 10 * displayBinary(dec / 2);       Console.Write(res);       return ... 阅读更多

使用 C# 将两个已排序的数组合并到列表中

Samual Sam
更新于 2020-06-22 12:55:32

218 次浏览

要将两个已排序的数组合并到列表中,首先设置两个已排序的数组 - int[] array1 = { 1, 2 }; int[] array2 = { 3, 4 }; 将其添加到列表并合并 - var list = new List(); for (int i = 0; i < array1.Length; i++) {    list.Add(array1[i]);       list.Add(array2[i]); } 现在,使用 ToArray() 方法转换回数组,如下所示 - 示例实时演示 using System; using System.Collections.Generic; public class Program {    public static void Main() {       int[] array1 = { 56, 70, 77};       int[] array2 = ... 阅读更多

C# 程序检查字符串是否包含所有元音

karthikeya Boyini
更新于 2020-06-22 12:55:59

435 次浏览

要检查所有元音,首先设置要检查的条件 - string res = str.Where(chk =< "aeiouAEIOU".Contains(chk)).Distinct(); 上面,我们使用了字符串 - string str = "the quick brown fox jumps over the lazy dog"; 现在,使用 Any() 方法检查字符串是否包含任何元音 - if(!res.Any()) Console.WriteLine("No vowels!"); 循环遍历字符串以获取元音 - 示例实时演示 using System; using System.Linq; public class Program {    public static void Main() {       string str = "the quick brown fox jumps over the lazy dog";       var res = str.Where(chk =< "aeiouAEIOU".Contains(chk)).Distinct();     ... 阅读更多

C# 程序将列表中的所有数字相乘

Samual Sam
更新于 2020-06-22 12:56:44

959 次浏览

首先,设置列表 - List myList = new List () {    5,    10,    7 }; 现在,将变量的值设置为 1,这将有助于我们进行乘法 - int prod = 1; 循环遍历并获取乘积 - foreach(int i in myList) {    prod = prod*i; } 以下是代码 - 示例实时演示 using System; using System.Collections.Generic; public class Program {    public static void Main() {       List myList = new List() {          5,          10,          7       };       Console.WriteLine("List: ");       foreach(int i in myList) {          Console.WriteLine(i);       }       int prod = 1;       foreach(int i in myList) {          prod = prod*i;       }       Console.WriteLine("Product: {0}",prod);    } } 输出 列表: 5 10 7 乘积: 350

C++ 和 C# 中的 Foreach

karthikeya Boyini
更新于 2020-06-22 12:57:28

607 次浏览

C++ 中的 Foreach C++ 11 引入了 foreach 循环来遍历每个元素。 这是一个示例 - 示例实时演示 #include using namespace std; int main() {    int myArr[] = { 99, 15, 67 };    // foreach 循环    for (int ele : myArr)    cout << ele << endl;    return 0; } 输出 99 15 67 C# 中的 Foreach C# 中的 Foreach 循环用于遍历集合中的每个元素。 这是一个示例 - 示例实时演示 using System; using System.Collections.Generic; public class Program {    public static void Main() {       List<int> list = new List<int>() { 1, 2, 3 };       foreach (int i in list) {          Console.WriteLine(i);       }    } }

在 C# 中打印 n 的前 m 个倍数

Samual Sam
更新于 2019-07-30 22:30:23

583 次浏览

要打印 n 的 m 个倍数,首先设置 m 和 n 的值 - int n = 6, m = 1; 现在循环遍历 m 的值,递增它并在每次迭代中乘以 n - while (m <= 5) {    Console.WriteLine(n * m);    m++; } 以下是代码 - 示例实时演示 using System; public class Program {    public static void Main() {       int n = 6, m = 1;       while (m <= 5) {         Console.WriteLine(n * m);         m++;       }    } }

在 C# 中打印带逗号作为千位分隔符的数字

Giri Raju
更新于 2020-06-22 12:57:50

2K+ 次浏览

首先,将数字设置为字符串 - string num = "1000000.8765"; 现在,分别处理小数点前后的数字 - string withoutDecimals = num.Substring(0, num.IndexOf(".")); string withDecimals = num.Substring(num.IndexOf(".")); 使用 ToString() 方法设置千位分隔符的格式 - ToString("#,##0") 以下是显示带逗号作为千位分隔符的数字的完整代码 - 示例实时演示 using System; public class Program {    public static void Main() {       string num = "1000000.8765";       string withoutDecimals = num.Substring(0, num.IndexOf("."));       string withDecimals = num.Substring(num.IndexOf("."));       withoutDecimals = Convert.ToInt32(withoutDecimals).ToString("#,##0");       Console.WriteLine(withoutDecimals + withDecimals);    } } 输出 1,000,000.8765

在 C# 中打印字符串中每个单词的首字母

usharani
更新于 2020-06-22 12:58:16

1K+ 次浏览

假设字符串为 - string str = "Never Give Up!"; 首先,分割每个单词 - string[] strSplit = str.Split(); 现在,循环遍历每个单词并使用子字符串方法显示首字母,如下面的代码所示 - 示例实时演示 using System; public class Program {    public static void Main() {       string str = "Never Give Up!";       Console.WriteLine("Initial String= "+str);       Console.WriteLine("Displaying first letter of each word...");       string[] strSplit = str.Split();       foreach (string res in strSplit) {          Console.Write(res.Substring(0,1));       }    } } 输出 初始字符串= Never Give Up! 显示每个单词的首字母... NGU

广告
© . All rights reserved.