acpig95 2017-10-21 01:02 采纳率: 0%
浏览 723

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条回答 默认 最新

  • qq5439 2017-10-23 12:47
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题