金陵科技学院实验报告
Console.WriteLine(s.Swap(a, b)); Console.WriteLine(s.Swap(ref a,ref b)); }
class Swaper {
public string Swap(int x, int y) {
int temp; temp = x; x = y; y = temp; return 后:x={0},y={1}\
}
public string Swap(ref int x, ref int y) {
int temp; temp = x; x = y; y = temp;
return string.Format(\按引用传参交换之后:x={0},y={1}\x, y);
} } }
}2、定义一个方法,实现数组的排序。 using System;
using System.Collections.Generic; using System.Text;
namespace second.two {
class Program {
string.Format(\
按
值
传
参
交
换
之
9
金陵科技学院实验报告
public class sort {
public void change(int[] a) {
Console.WriteLine(\排序前,数组顺序为:\ show(a); int i, j, m;
for (i = 0; i < 10; i++) {
m = a[i];
j = i - 1; //a[j]为数组前一个值
while (j >= 0 && m > a[j])//判断i下标的数是否大于j下标的数
{
a[j + 1] = a[j];//如果i下标大于j把j往后移一个位
j--; }
a[j+1] = m; //当不大于j的时候就把M的值放到i下标下面 j+1 是为了下标减到最前时考虑 -1 + 1 还是下标的最前面
}
Console.WriteLine(\排序后,数组顺序为:\ show(a); }
void show(int[] a) { int i;
for (i = 0; i < 10; i++) {
Console.Write(\ }
Console.WriteLine(); } }
10
金陵科技学院实验报告
static void Main(string[] args) {
int[] a ={ 4, 7, 1, 2, 5, 8, 9, 10, 3, 6 }; sort s=new sort(); s.change(a); } } }
3、定义一个学生类,把学生类当作对象来传递。
using System;
using System.Collections.Generic; using System.Linq; using System.Text;
namespace second.three {
class Program {
public class student {
public void st() {
int a = 999; } }
public class st {
public void aa(student s) {
Console.WriteLine(s); } }
static void Main(string[] args) {
11
金陵科技学院实验报告
student s=new student(); st s1 = new st(); s1.aa(s);
} } }
4、定义一个方法,求两个数的和和差,通过参数把这两个值带回。
using System;
using System.Collections.Generic; using System.Linq; using System.Text;
namespace second.four {
class Program {
public class sum {
public void ab(out int m, out int n,int a, int b) {
m = a + b; n = a - b; } }
static void Main(string[] args) {
sum s = new sum(); int a = 10; int b = 3; int m, n;
s.ab(out m, out n, a, b);
Console.WriteLine(\
12
金陵科技学院实验报告
} } }
5、用构造函数重载,实现矩形的面积,圆的面积,梯形的面积;
using System;
using System.Collections.Generic; using System.Linq; using System.Text;
namespace secong.five {
class Program {
public class square {
public double area; public square() { } public square(double a) {
area = a * a * 3.14; }
public square(double a, double b) {
area = a * b; }
public square(double a, double b, double h) {
area = (a + b) / 2 * h; } }
static void Main(string[] args) {
double a, b, h,area; a = 2; b = 5; h = 3;
13