C#数组(小练习)
来源:互联网 发布:windows自带录制视频 编辑:程序博客网 时间:2024/04/30 02:53
练习1:从一个整数数组中取出最大的整数
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 练习{ class Program { static void Main(string[] args) { //从一个整数数组中取出最大的整数 int[] numbers = { 3, 5, 6, 37, 19, 98, 65, 34, 54, 43 }; int max = numbers[0]; int min = numbers[0]; for (int i = 0; i < numbers.Length; i++) { if (numbers[i] > max) { max = numbers[i]; } if (numbers[i] < min) { min = numbers[i]; } } Console.WriteLine("max={0},max={1}", max, min); Console.ReadKey(); } }}
练习2:计算一个整数数组的所有元素的和。
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 练习{ class Program { static void Main(string[] args) { //计算一个整数数组的所有元素的和 int[] numbers = { 3, 5, 6, 37, 19, 98, 65, 34, 54, 43 }; int sum = 0; for (int i = 0; i < numbers.Length; i++) { //sum =sum+numbers[i]; sum += numbers[i]; } Console.WriteLine("sum=" + sum);//sum=364 Console.ReadKey(); } }}练习3:将一个字符串数组输出为|分割的形式,比如“梅西|卡卡|郑大世”
方法一:
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 练习{ class Program { static void Main(string[] args) { //将一个字符串数组输出为|分割的形式,比如“梅西|卡卡|郑大世” string[] names = { "梅西", " 卡卡", " 郑大世" }; string str = ""; for (int i = 0; i < names.Length; i++) { if (i == names.Length - 1)//循环到数组的最后一个元素 不需要加| { str = str + names[i]; } else { str = str + names[i] + "|";//循环后加两个元素之间 | } } Console.WriteLine(str); Console.ReadKey(); } }}方法二:
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 练习{ class Program { static void Main(string[] args) { //将一个字符串数组输出为|分割的形式,比如“梅西|卡卡|郑大世” string[] names = { "梅西", " 卡卡", " 郑大世" }; string str = ""; for (int i = 0; i < names.Length; i++) { if (i == names.Length - 1) { break;//循环到最后一个元素跳出 } str = str + names[i] + "|"; } str = str + names[names.Length - 1];//加上最后一个元素 Console.WriteLine(str); Console.ReadKey(); } }}练习4:将一个字符串数组的元素的顺序进行反转。{“3”,“a”,“8”,“haha”} {“haha”,“8”,“a”,“3”}。第i个和第length-i-1个进行交换。
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 练习{ class Program { static void Main(string[] args) { // 将一个字符串数组的元素的顺序进行反转。{“3”,“a”,“8”,“haha”} //{“haha”,“8”,“a”,“3”}。第i个和第length-i-1个进行交换。 string[] str = { "haha", "8", "a", "3" }; //反序输出 for (int i = str.Length - 1; i >= 0; i--) { Console.WriteLine(str[i]); } Console.WriteLine("======="); //交换反转 string temp; for (int i = 0; i < str.Length / 2; i++) { //第i个元素和length-1-i个元素 temp = str[i]; str[i] = str[str.Length - 1 - i]; str[str.Length - 1 - i] = temp; } for (int i = 0; i < str.Length; i++) { Console.WriteLine(str[i]); } Console.ReadKey(); } }}
0 0
- C#数组(小练习)
- C#小练习(输入10个数存入数组中,求最大值、最小值和平均值.)
- C# 小程序之新手练习(三)数组模拟约瑟夫环
- 对象数组小练习
- 数组小练习
- 三维数组小练习
- 数组方法小练习
- C#小练习(规律求数)
- C#小练习(猜数字)
- C# 入门小练习(持续更新)
- C#小练习
- C# List小练习
- C#小练习
- c#小练习ⅰ
- C#小练习ⅱ
- C#小练习ⅲ
- C#小练习ⅳ
- C#小练习ⅴ
- Linux终端用户的8个技巧
- 黑马程序员----学习经验
- 核心配置(Core Configuration)
- 如何将脚本放在页面里?(转载)
- 【中断异常】系统调用
- C#数组(小练习)
- [GitHub][2014-05-11 18:34:13]eqq-python
- tomcat缓存静态资源深入
- MySQL字段数据类型和长度
- Geogebra习作[Steiner chain动态观察]
- PHP html_entity_decode()
- ShotGraph例程
- 制作可以直接烧录的嵌入式镜像文件(Nand Flash)
- 黑马程序员-----初识.NET