我正在尝试制作一个练习程序来计算一个人的总成绩。测试由三个部分组成,每个部分具有不同的权重。第 1 部分为 5 分,第二部分为 3 分,第 3 部分为 2 分。
因此,如果一个人得到 A B C,他们将得到 5A 3B 和 2C。
现在,为了接收和 A/B/C 总体需要每个等级的一定数量。例如,要获得总分 A,您需要至少获得 5A,其中 7 个成绩必须为 B 或更高,并且所有成绩都必须为 C 或更高。
B、C、D等也都有自己的要求。
最好的编码方式是什么,因为目前我正在为每个年级使用一个计数器,然后执行 if/else if 语句来检查一个人获得的每个年级的数量,如下所示:
if (aGradeCount >= 5)
{
//Add total grade
}
}
}
//To receive a B
if(bGradeCount >= 3 && aGradeCount <5 && cGradeCount >=2)
{
if(bGradeCount + cGradeCount +dGradeCount + aGradeCount>= 7)
{
if(dGradeCount <= 3)
{
//Add new total grade
}
}
}
现在我明白这是一种糟糕的做法,但我怎样才能更好地编码呢?使用 switch 语句?如果是这样,我该怎么做?
最佳答案
how can I code this better?
编写规范。然后针对规范中提到的每个概念,编写一个方法。这是规范的一部分;你已经写好了:
in order to receive an A overall you need to have at least 5 A's and least 7 of the grades must be B or higher and all the grades need to be C or better.
分解
in order to receive an A overall
at least 5 A's AND
at least 7 of the grades must be B or higher AND
all the grades need to be C or better
好的,现在我们可以开始把它变成一个方法了:
static bool QualifiesForA(int aCount, int bCount, int cCount, int dCount)
{
// In order to receive an A overall we require:
// TODO: at least 5 A's AND
// TODO: at least 7 of the grades must be B or higher AND
// TODO: all the grades need to be C or better
// If these conditions are not met then an A is not earned.
return false;
}
好的,我们已经将规范转化为代码。错误的代码,但代码。我们继续吧。我们有一条规范线。写一个方法:
static bool AtLeastFiveA(int aCount)
{
return aCount >= 5;
}
嘿,这是一个正确的方法。我们正在取得进展。现在使用它:
static bool QualifiesForA(int aCount, int bCount, int cCount, int dCount)
{
// In order to receive an A overall we require:
// at least 5 A's AND
// TODO: at least 7 of the grades must be B or higher AND
// TODO: all the grades need to be C or better
bool atLeast5A = AtLeastFiveA(aCount);
// If these conditions are not met then an A is not earned.
return false;
}
现在我们有另一个问题。至少有 7 个是 B 或更高。 OK,写一个方法:
static bool AtLeastSevenB(int aCount, int bCount)
{
return aCount + bCount >= 7;
}
又一个正确的方法!使用它!
static bool QualifiesForA(int aCount, int bCount, int cCount, int dCount)
{
// In order to receive an A overall we require:
// at least 5 A's AND
// at least 7 of the grades must be B or higher AND
// TODO: all the grades need to be C or better
bool atLeast5A = AtLeastFiveA(aCount);
bool atLeast7B = AtLeastSevenB(aCount, bCount);
// If these conditions are not met then an A is not earned.
return false;
}
现在我们需要最后一点:
static bool NoD(int dCount)
{
return dCount == 0;
}
把它放在一起。
static bool QualifiesForA(int aCount, int bCount, int cCount, int dCount)
{
// In order to receive an A overall we require:
// at least 5 A's AND
// at least 7 of the grades must be B or higher AND
// all the grades need to be C or better
bool atLeast5A = AtLeastFiveA(aCount);
bool atLeast7B = AtLeastSevenB(aCount, bCount);
bool noD = NoD(dCount);
if (atLeast5A && atLeast7B && noD)
return true;
// If these conditions are not met then an A is not earned.
return false;
}
现在,要问自己的问题是:
这段代码是否正确?先把它弄好。这段代码非常冗长,但我现在就告诉您,它完全符合您提供的规范。
一旦代码正确了,我们能不能说得更清楚一些?
是的;例如,我们可以说:
static bool QualifiesForA(int aCount, int bCount, int cCount, int dCount)
{
// In order to receive an A overall we require:
// at least 5 A's AND
// at least 7 of the grades must be B or higher AND
// all the grades need to be C or better
bool atLeast5A = AtLeastFiveA(aCount);
bool atLeast7B = AtLeastSevenB(aCount, bCount);
bool noD = NoD(dCount);
return atLeast5A && atLeast7B && noD;
}
现在也许你会说,你知道,其中一些方法是不必要的抽象,也许我可以用它们的主体替换它们:
static bool QualifiesForA(int aCount, int bCount, int cCount, int dCount)
{
// In order to receive an A overall we require:
// at least 5 A's AND
// at least 7 of the grades must be B or higher AND
// all the grades need to be C or better
bool atLeast5A = aCount >= 5;
bool atLeast7B = aCount + bCount >= 7;
bool noD = dCount == 0;
return atLeast5A && atLeast7B && noD;
}
要点是:我们从一个非常冗长、清晰正确的程序开始,然后我们进行小的、简单的、清晰正确的转换,使其更加简洁。当您认为您在简洁性和可读性之间取得了很好的平衡时,请停下来。
好的,现在您已经解决了“我们获得 A 了吗?”的问题。现在你问“我们得了 B 吗?”等等。为每个部分编写规范,然后编写明确实现规范的代码。
这听起来像是一个重量级的过程,但在您学习如何编程时,这将带来巨大的好处。您的代码将组织得更好,错误更少,更易于阅读、理解和修改。
此技术的重点是关注每个部分的明显正确性。始终专注于明显的正确性。一个正确但你不能说它是正确的程序是一个可能不正确的程序!始终首先专注于正确性。使错误的程序更优雅、更快或功能更完整意味着您拥有一个优雅、快速、功能丰富的错误农场。
总结:
- 编写明确的规范。
- 编写代码以明确符合规范。
- 微型方法非常好。你以后总是可以消除它们。
- 正确比什么都重要;一旦您知道代码是正确的,就改进它。
关于C# 多个 If 语句替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35951391/