C#函数调用结果为什么不对

这是学生类。
class Student
{
public string id;
public string name;
public double chinese;
public double math;
public double english;
public Student()
{

    }
    public Student(string id,string name,double chinese,double math,double english)
    {
        this.id = id;
        this.name = name;
        this.chinese = chinese;
        this.math = math;
        this.english = english;
    }
    public string Id
    {
        set { this.id = value; }
        get{return this.id ;} 
    }
    public string Name
    {
        set { this.name = value; }
        get { return this.name; }
    }
    public double Chinese
    {
        set { this.chinese = value; }
        get { return this.chinese; }
    }
    public double Math
    {
        set { this.math = value; }
        get { return this.math; }
    }
    public double English
    {
        set { this.english = value; }
        get { return this .english ;}
    }
    public double Total{
        get { return chinese + math + english; }
    }

}
这是Grade类
class Grade
{
public Student[] students = new Student[3];
public int n;
public Grade ()
{
n = 0;
}
public void add(Student s)
{
students[n] = s;
n++;
}
public int search(string name)
{
int i;
for ( i=0;i {
if (students[i].name == name) break;
}
if (i == n) return -1;
else return i;
}
public void sort()
{
for(int i=0;i {
int k = i;
for (int j = i + 1; j if (students[j].Total > students[k].Total) k = j;
if (k != i)
{
Student temp;
temp = students[k];
students[k] = students[i];
students[i] = temp;
}
}
}
private string getMaxer1()
{
int max1 = 0;
for (int i = 1; i < n; i++)
if (students[i].chinese > students[max1].chinese) max1= i;
return students[max1].chinese+","+students[max1].name;
}
private string getMaxer2()
{
int max2 = 0;
for (int i = 1; i < n; i++)
if (students[i].math > students[max2].math) max2 = i;
return students[max2].chinese + "," + students[max2].name;
}
private string getMaxer3()
{
int max3 = 0;
for (int i = 1; i < n; i++)
if (students[i].english > students[max3].english) max3 = i;
return students[max3].chinese + "," + students[max3].name;
}
private string getLoser1()
{
string result = "";
for (int i = 0; i < n; i++)
{
students[i] = new Student();

            if (students[i].chinese < 60) result += students[i].name + "\n";
        }
        return result;
    }
    private string getLoser2()
    {
        string result = "";
        for (int i = 0; i < n; i++)
        {
            students[i] = new Student();
           if (students[i].math < 60) result += students[i].name + "\n";}
        return result;
    }
    private string getLoser3()
    {
        string result = "";
        for (int i = 0; i < n; i++)
        {
            students[i] = new Student();
            if (students[i].english < 60) result += students[i].name + "\n";
        }
        return result;
    }
    public string getMaxerAndLoser()
    {
        string Maxer = "", Loser = "";
        Maxer += "单科语文最高:" + getMaxer1() + "\n";
        Maxer += "单科数学最高:" + getMaxer2() + "\n";
        Maxer += "单科英语最高:" + getMaxer3() + "\n";
        Loser += "单科语文挂科名单:" + getLoser1() + "\n";
        Loser += "单科数学挂科名单:" + getLoser2() + "\n";
        Loser += "单科英语挂科名单:" + getLoser3() + "\n";
        return Maxer + "\n" + Loser;
    }
    public string all()
    {
        double allave = 0, all = 0;
        for(int i=0;i<3;i++)
        {
            students[i] = new Student();
            all = students[i].chinese + students[i].math + students[i].english;
            allave = allave + all;
        }
        return "平均成绩为:" + allave / n;
    }
    public double jige1()
    {
        double w = 0.0;
        for (int i = 0; i < 3; i++)
        {
            students[i] = new Student();
            if (students[i].chinese > 60) w += 1.0;
        }
        return w/n;
    }

       public double jige2() 
        {double w = 0.0;
        for (int i = 0; i < n; i++)
        {
            students[i] = new Student();

            if (students[i].math > 60) w += 1.0;
        }
            return w/n;
        }
        public double jige3()
       {
           double w = 0.0;
           for (int i = 0; i < 3; i++)
           {
               students[i] = new Student();
               if (students[i].english > 60) w += 1.0;
           }            
        return w / n;
    }
    public string jigelv()
    {
        string pass = "";
        pass += "语文及格率为:" + jige1() + "\n";
        pass += "数学及格率为:" + jige2() + "\n";
        pass += "英语及格率为:" + jige3() + "\n";
        return pass;
    }
}

}

这是主函数
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace P111实验3
{
public partial class Form1 : Form
{
Student[] students = new Student[3];
Grade ss = new Grade();
int n=0;
public Form1()
{
InitializeComponent();
}
int i = 0;
private void button1_Click(object sender, EventArgs e)
{
students[i] = new Student();//创建对象
students[i].id = textBox1.Text;
students[i].name = textBox2.Text;
students[i].chinese = Convert.ToDouble(textBox3.Text);
students[i].math = Convert.ToDouble(textBox4.Text);
students[i].english = Convert.ToDouble(textBox5.Text);
// n++;
// ss.add();
label7.Text = "已成功添加一个学生!此学生信息为:";
label7.Text += "学号:" + students[i].id + ",姓名:" + students[i].name + ",语文成绩:" + students[i].chinese + ",数学成绩:" + students[i].math + ",英语成绩:" + students[i].english;
label7.Text += "总成绩为:" + (students[i].chinese + students[i].math + students[i].english)+"\n";
i ++;
}

    private void button2_Click(object sender, EventArgs e)
    {
        foreach(Student c in students){
            label7.Text = "学号:" + c.id + ",姓名:" +c.name + ",语文成绩:" + c.chinese + ",数学成绩:" + c.math + ",英语成绩:" + c.english;
            label7.Text += "总成绩为:" + (c.chinese + c.math + c.english)+"\n";
        }
       label7.Text += "";      
        ss.sort();
        label7.Text += ss.getMaxerAndLoser();
        label7.Text += ss.all()+"\n";
        label7.Text += ss.jige1();
        label7.Text += ss.jige2();
        label7.Text += ss.jige3();
        label7.Text += ss.jigelv();
    }

    private void button3_Click(object sender, EventArgs e)
    {
       // label7.Text = ss.search();
    }
}

}
结果为截图2.这是怎么回事?谢谢谢谢图片说明

2个回答

打断电调试看下数据走的逻辑对不对就完了

数据格式转换出问题了,dubug 打断点,调试。一行行看代码有点困难啊

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!