📄 8-2.cs
字号:
//程序8-2
using System;
namespace School
{
public class Student
{
private string studName;
private int studID;
private string Department;
public Student(){}
public Student(string str, int id, string dept)
{
studName=str;
studID=id;
Department=dept;
}
public void FindInfo(int id)
{
if(id==studID)
Console.WriteLine("{0}is a student in the {1} Department ", studName,Department);
}
//params参数实际上是不定长一维数组或交错数组,只能作为参数列表的最后一个参数。
//定义形参使用params关键字,调用时不用该关键字
//购书费用计算
public double BooksTotleFee (params double [] bookFee)
{
float totle=0f;
foreach(float fee in bookFee) totle+=fee;
return totle;
}
//out参数与ref类似,直接将引用传递给方法,不在存储器中为参数开设新的空间。
//不同之处是:调用前不需要对参数作初始化,在方法结束前必须对out参数赋值。
// ave: 平均成绩;specave:加权平均成绩;score[0][]={"课程名","学分","成绩"};....
public void SpecAverage(out double ave,out double specave,params string [][] score)
{
double creditsum=0.0,scoresum=0.0,specscoresum=0.0;
int n=0;
foreach(string [] tt in score)
{
Console.Write("课程名: {0},学分: {1}, 成绩: {2}\n",tt[0],tt[1],tt[2]);
n++;
creditsum+=Convert.ToDouble(tt[1]); //学分累计
scoresum+=Convert.ToDouble(tt[2]); //成绩累计
specscoresum+=Convert.ToDouble(tt[1])*Convert.ToDouble(tt[2]); //加权成绩=学分×成绩
}
ave=scoresum/n; //在方法结束前必须对out参数赋值
specave=specscoresum/creditsum; //在方法结束前必须对out参数赋值
}
}
public class Test
{
public static void Main()
{
double Ave,SpecAve;
Student AStudent=new Student("张三", 0518288,"计算机");
Student AnotherStudent = new Student("李四",0518277,"计算机");
AStudent.FindInfo(0518288);
//调用前不需对Ave和SpecAve赋值,这是与ref不同点。
AStudent.SpecAverage(out Ave,out SpecAve,new string [3][]{new string []{"JAVA程序设计","4","87"},new string[] {".NET应用开发","6","79"},new string[] {"Web开发技术","4","89"}});
Console.WriteLine("平均成绩:{0} 加权平均成绩: {1}", Ave,SpecAve);
Console.WriteLine("书费:"+ AStudent.BooksTotleFee(36.5, 41.5, 38));
AnotherStudent.FindInfo(0518277);
string [][] sss=new string [2][]{new string[]{"体育","2","100"},new string[]{"法律与道德","4","60"}};
AnotherStudent.SpecAverage(out Ave, out SpecAve,sss);
Console.WriteLine("平均成绩:{0} 加权平均成绩: {1}", Ave,SpecAve);
Console.WriteLine("书费:"+ AnotherStudent.BooksTotleFee(30.5, 11, 28));
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -