数组和字符串.pptVIP

  • 2
  • 0
  • 约1.03万字
  • 约 43页
  • 2019-08-29 发布于广东
  • 举报
* * * * * * * * 解析:这段程序的关键点在于遍历整个班级的成绩时,将第一名的成绩和最后一名的成绩保留下来。代码如下所示。 using System; public class ScoreSample { public int GetFirstScore(int[] list) { int firstScore = 0; foreach (int i in list) { if (i firstScore) firstScore = i; } return firstScore; } public int GetLastScore(int[] list) { int lastScore = int.MaxValue; foreach (int i in list) { if (i lastScore) lastScore = i; } return lastScore; } } class Tester { static void Main(string[] args) { ScoreSample ss = new ScoreSample(); int[] scores = new int[] { 60, 70, 80, 90 }; Console.WriteLine(最高分是: + ss.GetFirstScore(scores)); Console.WriteLine(最低分是: + ss.GetLastScore(scores)); } } * * * * * * * * * 我们讲到了所有的基类就是Array类,通过具体的实例我们了解了Array类的一些常用成员和方法。 另外String类本身可以被看作是一个System.Char对象的数组,我们学习了如何对字符串来进行比较、分割和复制。 本章内容重点掌握数组的概念和Array类,要熟练的运用它们的具体操作和方法。 * * 在本实验中,我们将使用声明和使用数组。 请参照实验手册,并在教师指导下完成实验报告。 * * 数组 类型 foreach 索引 二维 Length * False,一个数组的元素只能是一种类型的数据。 False,一般是整型的。 False,单个数值元素传入方法,传入的只是它的副本。修改它的值不会影响倒数组里的值。 * using System; public class SamplesArray { public static void Main() { double[] values = new double[10]; values[3] = 1.667; values[9] = 3.333; double count = 0; foreach (double val in values) { count += val; } Console.WriteLine(count / values.Length); } } * public class Program { static void Main() { Console.WriteLine(请输入要拆分的句子:); string text = Console.ReadLine(); int lastSpaceIndex = 0; for (int i = 0; i text.Length; i++) { if (text[i] == || text[i] == , || text[i] == .) { Console.WriteLine(text.Substring(lastSpaceIndex, i - lastSpaceIndex)); lastSpaceIndex = i + 1; } } } } 多媒体演示: Copy方法 多媒体演示 示例代

文档评论(0)

1亿VIP精品文档

相关文档