| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1277 人关注过本帖
标题:C#中构造函数怎么使用?
只看楼主 加入收藏
liangshizhuo
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2012-9-5
结帖率:100%
收藏
已结贴  问题点数:2 回复次数:12 
C#中构造函数怎么使用?
1.设计一个学生成绩类,数据成员包括:班级,姓名,英语成绩、c#成绩、数据库;
方法成员包括:有参构造函数(参数为:班级,姓名,英语成绩、c#成绩、数据库),
计算总分,计算平均分,判断等级(平均在85,且各科成绩在75分以上为“优秀”
平均在75,且各科成绩在70分以上为“良好”其它“合格”)
2.采用WinForm形式设计程序,界面根据需要自行设计(考虑输入与输出),所有方法都要使用。
搜索更多相关主题的帖子: 英语成绩 设计程序 数据库 
2012-09-11 23:04
ZWYyj
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:9
帖 子:124
专家分:587
注 册:2012-8-10
收藏
得分:1 
class MyScore
{
    string grade;
    string name;
    double engScore;
    double cSharpScore;
    string database;
    public MyScore(string Grade,string Name,double EngScore,double CSharpScore,string Database)
    {
        this.grade=Grade;
        this.name=Name;
        this.engScore=EngScore;
        this.cSharpScore=CSharpScore;
    `   this.database=Database;
    }
    ····//再接着写一些你说的计算总分方法、平均分方法等
}
构造函数与包含它的类同名,以上的就是有参构造函数。在应用程序主入口函数中实例化一个对象,再调用类里面的方法。
2012-09-12 09:30
liangshizhuo
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2012-9-5
收藏
得分:0 
实例化class student=new class();是这样吗?求继续赐教!
2012-09-12 13:19
ZWYyj
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:9
帖 子:124
专家分:587
注 册:2012-8-10
收藏
得分:0 
MyScore myscore=new MyScore("二班","张三", 89.8, 90.0, 97.5);
上面的具体值对应相应的参数。然后再后面调用你所写的方法,最后就可以输出二班的张三总分多少,平均分多少等等···
2012-09-12 14:52
ZWYyj
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:9
帖 子:124
专家分:587
注 册:2012-8-10
收藏
得分:0 
你找找资料看看C#类的知识吧。这个算基础了。
2012-09-12 14:53
liangshizhuo
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2012-9-5
收藏
得分:0 
{
            string grade = Convert.ToString(textBox1.Text.Trim());
            string name = Convert.ToString(textBox2.Text.Trim());
            double sql = Convert.ToDouble(textBox3.Text.Trim());
            double english = Convert.ToDouble(textBox4.Text.Trim());
            double csharp = Convert.ToDouble(textBox5.Text.Trim());
            student h = new student(  grade,name,sql,english,csharp);
            label6.Text = Convert.ToString(h.zongfen(english, csharp,sql));
           

        }
        class student
        {
            string grade;
            string name;
            double sql;
            double english;
            double csharp,pj;
            public student(string grade, string name, double sql, double english, double csharp)
            {
                this.grade = grade;
                this.name = name;
                this.sql = sql;
                this.english = english;
                this.csharp = csharp;
            }
            public double zongfen(double english, double csharp,double sql)
            {
                return english + csharp+sql ;
            }
            public double average(double english, double csharp,double sql)
            {
                return pj = (english + csharp+sql) / 3;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string grade = Convert.ToString(textBox1.Text.Trim());
            string name = Convert.ToString(textBox2.Text.Trim());
            double  sql = Convert.ToDouble(textBox3.Text.Trim());
            double english = Convert.ToDouble(textBox4.Text.Trim());
            double csharp = Convert.ToDouble(textBox5.Text.Trim());
            student h = new student(grade, name, sql, english, csharp);
            label7.Text = Convert.ToString(h.average(english, csharp,sql));
        }
还可以怎么改进?肚子里的墨水太少,初次接触c#,另外判断等级的带码不怎么会写。求赐教!
2012-09-13 10:59
sunxuan007
Rank: 1
等 级:新手上路
帖 子:17
专家分:1
注 册:2012-8-28
收藏
得分:1 
判断等级可以用if else 加&&
if(平均分>85&&英语分>80&&...)
{console.writeline("优秀");
}
else if(平局分>80&&英语分>80&&...)
{console.writelin("良");
}
else if
。。。
2012-09-13 13:13
liangshizhuo
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2012-9-5
收藏
得分:0 
回复 4楼 ZWYyj
{
            string grade = Convert.ToString(textBox1.Text.Trim());
            string name = Convert.ToString(textBox2.Text.Trim());
            double sql = Convert.ToDouble(textBox3.Text.Trim());
            double english = Convert.ToDouble(textBox4.Text.Trim());
            double csharp = Convert.ToDouble(textBox5.Text.Trim());
            student h = new student(  grade,name,sql,english,csharp);
            label6.Text = Convert.ToString(h.zongfen(english, csharp,sql));
           

        }
        class student
        {
            string grade;
            string name;
            double sql;
            double english;
            double csharp,pj;
            public student(string grade, string name, double sql, double english, double csharp)
            {
                this.grade = grade;
                this.name = name;
                this.sql = sql;
                this.english = english;
                this.csharp = csharp;
            }
            public double zongfen(double english, double csharp,double sql)
            {
                return english + csharp+sql ;
            }
            public double average(double english, double csharp,double sql)
            {
                return pj = (english + csharp+sql) / 3;
            }
            public string dengji(double english, double csharp, double sql)
            {
                if (pj >= 85 && english > 75 && csharp > 75 && sql > 75)
                {
                    return "优秀";
                }
                else if (pj >= 75 && english > 70 && csharp > 70 && sql > 70)
                {
                    return "良好";
                }
                else
                {
                    return "合格";
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string grade = Convert.ToString(textBox1.Text.Trim());
            string name = Convert.ToString(textBox2.Text.Trim());
            double  sql = Convert.ToDouble(textBox3.Text.Trim());
            double english = Convert.ToDouble(textBox4.Text.Trim());
            double csharp = Convert.ToDouble(textBox5.Text.Trim());
            student h = new student(grade, name, sql, english, csharp);
            label7.Text = Convert.ToString(h.average(english, csharp,sql));
        }
      
        private void button3_Click(object sender, EventArgs e)
        {
           string grade = Convert.ToString(textBox1.Text.Trim());
            string name = Convert.ToString(textBox2.Text.Trim());
            double  sql = Convert.ToDouble(textBox3.Text.Trim());
            double english = Convert.ToDouble(textBox4.Text.Trim());
            double csharp = Convert.ToDouble(textBox5.Text.Trim());
            student h = new student(grade, name, sql, english, csharp);
            label8.Text = Convert.ToString(h.dengji(english, csharp,sql));
        }
      
            




    }
}
为什么无论我输入多少分成绩都是显示为合格的?显示不了优秀或良好的,到底哪里出错了?求大神指教!!!感激不尽!!
2012-09-13 15:32
ZWYyj
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:9
帖 子:124
专家分:587
注 册:2012-8-10
收藏
得分:0 
首先是你的判断语句里pj >= 85 && english > 75 && csharp > 75 && sql > 75中&&的意思是有四个条件都必须为True才返回优秀。
                if (pj >= 85 && english > 75 && csharp > 75 && sql > 75)
                {
                    return "优秀";
                }
                else
                {
                    if (pj >= 75 && english > 70 && csharp > 70 && sql > 70)
                    {
                        return "良好";
                    }
                    else
                        return "合格";
                }
2012-09-14 12:04
sunxuan007
Rank: 1
等 级:新手上路
帖 子:17
专家分:1
注 册:2012-8-28
收藏
得分:0 
在判断方法里pj不是字段,只是前面的局域变量
if (pj >= 85 && english > 75 && csharp > 75 && sql > 75)
改成下面会不会好使
if (average(double english, double csharp,double sql) >= 85 && english > 75 && csharp > 75 && sql > 75)
仅供参考
            
2012-09-15 16:04
快速回复:C#中构造函数怎么使用?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.051212 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved