标题:C#刚开始学 问问大神们在score属性封装中的if语句为什么没有执行?
只看楼主
夏生00
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2016-11-21
结帖率:0
已结贴  问题点数:20 回复次数:2 
C#刚开始学 问问大神们在score属性封装中的if语句为什么没有执行?
public class student
    {
private int Id;
private string name;

public string Name
{
    get { return name; }
    set { name = value; }
}
private DateTime brithday;

public DateTime Brithday
{
    get { return brithday; }
    set { brithday = value; }
}
private int score;

public int Score
{
    get { return score; }
    set { if (score>100) score =100;else if (score>=0) score = value;else score=0 ; }

public string dj;
public student() { }
public student(int xh, string xm)
{ Id = xh; name = xm; }
public student(int xh, string xm, DateTime bri, int cj)
{ Id = xh; name = xm; brithday = bri; cj = score; }
public void inscore()
          {
            Console.Write("学号:");
            Id = int.Parse(Console.ReadLine());
            Console.Write("姓名:");
            name = Console.ReadLine();
            Console.Write("生日:");
            brithday = Convert.ToDateTime(Console.ReadLine());
            Console.Write("总分:");
            score = int.Parse(Console.ReadLine());
           }
public string ScoreDj(int cj)
{
    string dj;
           if (cj>= 90) dj = "优秀";
           else if (cj>= 80) dj = "良好";
           else if (cj>= 70) dj = "中等";
           else if (cj>= 60) dj = "及格";
           else dj = "不及格";
           return dj;
 }

public void Display()
{ Console.WriteLine("   {0},    {1},   {2},   {3},    {4}", Id, name, brithday, score, ScoreDj(score)); }
 }
class Program
    {
const int Max = 100;
static void sort(int n, params student[] p)     //采用冒泡排序法排序
            {
int i, j;
student tmp;
for (i = 0; i< n - 1; i++)
                {
for (j = 0; j < n - i - 1; j++)
if (p[j + 1].Score> p[j].Score)
{ tmp = p[j + 1];                       //p[j+1]<->p[j]
p[j + 1] = p[j];
                            p[j] = tmp;
                      }
   }
 }
static void Main(string[] args)
{  int n, i;
student[] p = new student[Max];     //定义对象引用数组
Console.Write("n:");
                    n = int.Parse(Console.ReadLine());
for (i = 0; i< n; i++)             //创建对象引用的实例
                        p[i] = new student();
for (i = 0; i< n; i++)
                    {
Console.WriteLine("输入第{0}个学生数据:", i + 1);
                        p[i].inscore();
                    }
Console.WriteLine("排序前:");
Console.WriteLine("\t学号\t姓名\t生日\t成绩\t成绩等级");
for (i = 0; i< n; i++)
                    {
Console.Write("序号{0}:", i + 1);
                        p[i].Display();
                    }
sort(n, p);                     //按总分降序排序
Console.WriteLine("排序后:");
Console.WriteLine("\t学号\t姓名\t生日\t成绩\t成绩等级");
for (i = 0; i< n; i++)
                    {
Console.Write("第{0}名:", i + 1);
                        p[i].Display();
                    }
Console.Read();
                }
    }
}
搜索更多相关主题的帖子: private public return 
2016-11-21 16:34
yhlvht
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:36
帖 子:707
专家分:4405
注 册:2011-9-30
得分:20 
只发现这一句score = int.Parse(Console.ReadLine());是给score赋值,但是你使用的是字段score,并没有使用属性Score,没有使用怎么会执行
2016-11-21 19:23
夏生00
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2016-11-21
得分:0 
回复 2楼 yhlvht
谢谢版主大神~~~~
2016-11-21 21:11



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-471419-1-1.html




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

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