C#设计一个成绩类,急求!!!
答案:1 悬赏:30 手机版
解决时间 2021-03-02 09:02
- 提问者网友:轮囘Li巡影
- 2021-03-01 22:13
C#设计一个成绩类,急求!!!
最佳答案
- 五星知识达人网友:山有枢
- 2021-03-01 23:50
public class Score_Zqj
{
public string Name{get;set;}
public string ClassName{get;set;}
public string Subject{get;set;}
public double Score{get;private set;}
public bool IsEffectiveScore
{
get
{
return Score >= 0 && Score <= 100
}
}
public Score_Zqj()
{
}
public Score_Zqj(string name, strign className,string subject)
{
Name = name;
ClassName = className;
Subject = subject;
}
public void InputScore()
{
double score = 0;
if(double.TryParse(Console.ReadLine(),out score))
{
Score = score;
if(!IsEffectiveScore)
Score = 0;
}
}
public void UpdateScore()
{
double score = 0;
Console.WriteLine("please input score: ");
if (double.TryParse(Console.ReadLine(), out score))
{
Score = score;
while (!IsEffectiveScore)
{
Console.WriteLine("Invalid score, please input again");
UpdateScore();
}
}
}
public void OutputScore()
{
string outputMessage = "{0} is in Class {1}, {0}'s {2} score is {3} points.";
Console.WriteLine(string.Format(outputMessage, Name, ClassName, Subject, Score));
}
}上述例子只是为了让你直观的看到效果 实际上UpdateScore里面不应该有输入不对重复输入的逻辑 再给你一个例子
public class Score_Zqj
{
public string Name { get; set; }
public string ClassName { get; set; }
public string Subject { get; set; }
public double Score { get; private set; }
public bool IsEffectiveScore
{
get
{
return 0 <= Score && Score <= 100;
}
}
public Score_Zqj()
{
}
public Score_Zqj(string name, string className, string subject)
{
Name = name;
ClassName = className;
Subject = subject;
}
public void InputScore(double inputScore)
{
Score = inputScore;
if (!IsEffectiveScore)
{
Score = 0;
}
}
public bool UpdateScore(double inputScore)
{
bool result = true;
Score = inputScore;
if (!IsEffectiveScore)
{
result = false;
}
return result;
}
public void OutputScore()
{
string outputMessage = "{0} is in Class {1}, {0}'s {2} score is {3} points.";
Console.WriteLine(string.Format(outputMessage, Name, ClassName, Subject, Score));
}
}你在调用UpdateScore方法的地方加逻辑判断 只要返回值不为真 就需要一直输入 直到对为止 下面给你补上调用吧
static void main(string[] args)
{
Score_Zqj[] szs = new Score_Zqj[5];
for (int i = 0; i < szs.Length; i++)
{
Score_Zqj sz = new Score_Zqj("zhangsan" + i, "3(2)", "english");
double score = 0;
if (double.TryParse(Console.ReadLine(), out score))
{
while (!sz.UpdateScore(score))
{
double.TryParse(Console.ReadLine(), out score);
}
}
szs[i] = sz;
}
foreach (Score_Zqj sz in szs)
{
sz.OutputScore();
}
}
{
public string Name{get;set;}
public string ClassName{get;set;}
public string Subject{get;set;}
public double Score{get;private set;}
public bool IsEffectiveScore
{
get
{
return Score >= 0 && Score <= 100
}
}
public Score_Zqj()
{
}
public Score_Zqj(string name, strign className,string subject)
{
Name = name;
ClassName = className;
Subject = subject;
}
public void InputScore()
{
double score = 0;
if(double.TryParse(Console.ReadLine(),out score))
{
Score = score;
if(!IsEffectiveScore)
Score = 0;
}
}
public void UpdateScore()
{
double score = 0;
Console.WriteLine("please input score: ");
if (double.TryParse(Console.ReadLine(), out score))
{
Score = score;
while (!IsEffectiveScore)
{
Console.WriteLine("Invalid score, please input again");
UpdateScore();
}
}
}
public void OutputScore()
{
string outputMessage = "{0} is in Class {1}, {0}'s {2} score is {3} points.";
Console.WriteLine(string.Format(outputMessage, Name, ClassName, Subject, Score));
}
}上述例子只是为了让你直观的看到效果 实际上UpdateScore里面不应该有输入不对重复输入的逻辑 再给你一个例子
public class Score_Zqj
{
public string Name { get; set; }
public string ClassName { get; set; }
public string Subject { get; set; }
public double Score { get; private set; }
public bool IsEffectiveScore
{
get
{
return 0 <= Score && Score <= 100;
}
}
public Score_Zqj()
{
}
public Score_Zqj(string name, string className, string subject)
{
Name = name;
ClassName = className;
Subject = subject;
}
public void InputScore(double inputScore)
{
Score = inputScore;
if (!IsEffectiveScore)
{
Score = 0;
}
}
public bool UpdateScore(double inputScore)
{
bool result = true;
Score = inputScore;
if (!IsEffectiveScore)
{
result = false;
}
return result;
}
public void OutputScore()
{
string outputMessage = "{0} is in Class {1}, {0}'s {2} score is {3} points.";
Console.WriteLine(string.Format(outputMessage, Name, ClassName, Subject, Score));
}
}你在调用UpdateScore方法的地方加逻辑判断 只要返回值不为真 就需要一直输入 直到对为止 下面给你补上调用吧
static void main(string[] args)
{
Score_Zqj[] szs = new Score_Zqj[5];
for (int i = 0; i < szs.Length; i++)
{
Score_Zqj sz = new Score_Zqj("zhangsan" + i, "3(2)", "english");
double score = 0;
if (double.TryParse(Console.ReadLine(), out score))
{
while (!sz.UpdateScore(score))
{
double.TryParse(Console.ReadLine(), out score);
}
}
szs[i] = sz;
}
foreach (Score_Zqj sz in szs)
{
sz.OutputScore();
}
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯