
//定义 int 类型的数组
int[] a = {1,2,3};
//输岀数组中的一个元素
Console.WriteLine(a[0]);
//输出数组中的最后一个元素
Console.WriteLine(a[a.Length-1]);
class Program
{
static void Main(string[] args)
{
int[] a = new int[5];
Console.WriteLine("请输入5个整数:");
for(int i = 0; i < a.Length; i++)
{
a[i] = int.Parse(Console.ReadLine());//将字符串类型转换成整型
}
int max = a[0];//这里假设a[0]是最大的
for(int i = 1; i < a.Length; i++)
{
if (a[i] > max)
{
max = a[i];
}
}
Console.WriteLine("数组中最大值为:" + max);
}
}
class Program
{
static void Main(string[] args)
{
double[,] points = { { 90, 80 }, { 100, 89 }, { 88.5, 86 } };
for(int i = 0; i < points.GetLength(0); i++)
{
Console.WriteLine("第" + (i + 1) + "个学生成绩:");
for(int j = 0; j < points.GetLength(1); j++)
{
Console.Write(points[i, j] + " ");
}
Console.WriteLine();
}
}
}
class Program
{
static void Main(string[] args)
{
int[][] arrays = new int[3][];
arrays[0] = new int[] { 1, 2 };
arrays[1] = new int[] { 3, 4, 5 };
arrays[2] = new int[] { 6, 7, 8, 9 };
for(int i = 0; i < arrays.Length; i++)
{
Console.WriteLine("输出数组中第" + (i + 1) + "行的元素:");
for(int j=0;j<arrays[i].Length; j++)
{
Console.Write(arrays[i][j] + " ");
}
Console.WriteLine();
}
}
}
int[][] arrays = new int[3][];
arrays[0] = new int[2];
arrays[1] = new int[3];
arrays[2] = new int[4];
for(int i = 0; i < arrays.Length; i++)
{
Console.WriteLine("输入数组中第" + (i + 1) + "行的元素:");
for(int j=0;j<arrays[i].Length; j++)
{
arrays[i][j] = int.Parse(Console.ReadLine());
}
Console.WriteLine();
}
class Program
{
static void Main(string[] args)
{
double[] points = { 80, 88, 86, 90, 75.5 };
double sum = 0;
double avg = 0;
foreach(double point in points)
{
sum = sum + point;
}
avg = sum / points.Length;
Console.WriteLine("总成绩为:" + sum);
Console.WriteLine("平均成绩为:" + avg);
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入一个字符串:");
string str = Console.ReadLine();
string[] condition = { "," };
string[] result = str.Split(condition, StringSplitOptions.None);
Console.WriteLine("字符串中含有逗号的个数为:" + (result.Length - 1));
}
}
class Program
{
static void Main(string[] args)
{
int[] a = { 5, 1, 7, 2, 3 };
for(int i = 0; i < a.Length; i++)
{
for(int j = 0; j < a.Length - i - 1; j++)
{
if (a[j] > a[j + 1])
{
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
Console.WriteLine("升序排序后的结果为:");
foreach(int b in a)
{
Console.Write(b + "");
}
Console.WriteLine();
}
}
数组中常用的方法如下表所示。
| 编号 | 方法 | 描述 |
|---|---|---|
| 1 | Clear() | 清空数组中的元素 |
| 2 | Sort() | 冒泡排序,从小到大排序数组中的元素 |
| 3 | Reverse() | 将数组中的元素逆序排列 |
| 4 | IndexOf() | 查找数组中是否含有某个元素,返回该元素第一次出现的位置,如果没有与之匹配的元素,则返回 -1 |
| 5 | LastIndexOf() | 查找数组中是否含有某个元素,返回该元素最后一次出现的位置 |
class Program
{
static void Main(string[] args)
{
int[] a = { 5, 3, 2, 4, 1 };
Array.Sort(a);
Console.WriteLine("排序后的结果为:");
foreach(int b in a)
{
Console.Write(b + " ");
}
Console.WriteLine();
}
}
class EnumTest
{
public enum Title : int
{
助教,
讲师,
副教授,
教授
}
}
调用:
class Program
{
static void Main(string[] args)
{
Console.WriteLine(EnumTest.Title.助教 + ":" + (int)EnumTest.Title.助教);
Console.WriteLine(EnumTest.Title.讲师 + ":" + (int)EnumTest.Title.讲师);
Console.WriteLine(EnumTest.Title.副教授 + ":" + (int)EnumTest.Title.副教授);
Console.WriteLine(EnumTest.Title.教授 + ":" + (int)EnumTest.Title.教授);
}
}
class EnumTest
{
public enum Title : int
{
助教=1,
讲师,
副教授=4,
教授
}
}
class Program
{
static void Main(string[] args)
{
student stu = new student();
stu.Name = "张三";
stu.Age = -100;
Console.WriteLine("学生的信息为:");
Console.WriteLine(stu.Name + ":" + stu.Age);
}
struct student
{
private string name;
private int age;
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public int Age
{
get
{
return age;
}
set
{
if (value < 0)
{
value = 0;
}
else
{
age = value;
}
}
}
}
}
class Program
{
static void Main(string[] args)
{
student stu = new student("李四",25);
stu.PrintStudent();
}
}
public struct student
{
public student(string name, int age)
{
this.name = name;
this.age = age;
}
private string name;
private int age;
public void PrintStudent()
{
Console.WriteLine("姓名:" + name);
Console.WriteLine("年龄:" + age);
}
}
| 结构体 | 类 |
|---|---|
| 允许不使用new对其实例化 | 必须使用new实例化 |
| 没有默认构造方法 | 有默认构造方法 |
| 不能继承类 | 能继承类 |
| 没有析构方法 | 有析构方法 |
| 不允许使用abstract、protected以及sealed修饰 | 允许使用abstract、protected以及sealed修饰 |
下一篇:没有了
讨论数量:0