c#练手code

```using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication11
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(" put into  3 numbers");
int a, b, c;
display(a, b, c);
}

static void  display(int a, int b, int c)
{
//保证a最大
if(a<b)
swap(ref a, ref b);
if(a<c)
swap(ref a, ref c);
//保证b第二大
if(b<c)
swap(ref b, ref c);
Console.WriteLine("{0},{1},{2}", a, b, c);
}

// 交换函数
static void swap(ref int a,ref int b)
{
int temp = a;
a = b;
b = temp;
}

}
}```
``` put into  3 numbers
55
88
111
111,88,55

```using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace homework1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(" put into  3 numbers");
int a,b,c;

Console.WriteLine("the array:");
int[] num = { a, b, c };
display(num);
sort(num);
Console.WriteLine(" sort the end");
display(num);

}

static void sort(int[] str)
{
for(int i=0; i<str.Length-1; i++)
for (int j = 0; j < str.Length - i - 1; j++)
{
if (str[j] < str[j + 1])
{
int temp = str[j];
str[j] = str[j + 1];
str[j + 1] = temp;
}

}

}

static void display(int[] str)
{

foreach (int n in str)
Console.WriteLine(n);
}

}
}```
``` put into  3 numbers
11
22
66
the array:
11
22
66
sort the end
66
22
11

```using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace homework3
{
class Program
{

struct Student
{
public string name; //姓名
public float math; //数学成绩
}

static void Main(string[] args)
{

Student[] s = new Student[4];
s= input(s);
display(s);
//  double ave = s[0].math + s[1].math + s[2].math;
float ave = s[s.Length - 1].math;
Console.WriteLine("the average mark is :" + ave/3);
Console.WriteLine();

}

static Student[] input(Student[] s)
{
float sum = 0;
for(int i=0; i<s.Length-1; i++)
{

Console.WriteLine("put "+(i+1)+" student name and mark:");
sum += s[i].math;
}
s[s.Length - 1].math = sum;
return s;
}

static void display(Student[] s)
{
for(int i=0; i<s.Length; i++)
{
Console.WriteLine("{0}的数学成绩是:{1}",s[i].name,s[i].math);
}
}

}
}```

```put 1 student name and mark:
a
85
put 2 student name and mark:
b
90.5
put 3 student name and mark:
c
76.5
a的数学成绩是:85
b的数学成绩是:90.5
c的数学成绩是:76.5

the average mark is :84```

132 篇文章37 人订阅

0 条评论

相关文章

31610

295100

393130

C#中格式化数据的输出

alignment:可选，是一个带符号的整数，指示首选的格式化字段宽度。如果“对齐”值小于格式化字符串的长度，“对齐”会被忽略，并且使用格式化字符串的长度作为字...

11500

LINQ分页和排序,skip和Take 用法

LINQ分页和排序,skip和Take 用法 dbconn.BidRecord.OrderBy(p=>p.bid_id).ToList<BidRecord>()...

40490

make Entity Framework revert empty strings to null!

public override int SaveChanges(SaveOptions options)         {             forea...

29260

32870

34810

71150