我目前正在编写一个成绩计算器,可以增加3个考试成绩,并根据平均成绩给出一个字母等级。到目前为止,除了一件事,我已经能够编码所有内容而没有任何问题。在这个计算器中,我添加了一个复选框,它会降低最低等级,如果选中则会平均最高等级2。几周前我刚开始编程,不知道如何开始编码。我应该使用if语句还是循环?如果你们有任何关于消除不必要的代码或其他任何东西的提示,请随意指出。
public frmGradeCalculator()
{
InitializeComponent();
}
private void chkDropLowest_CheckedChanged(object sender, EventArgs e)
{
}
private void btnClear_Click(object sender, EventArgs e)
{
// Clear Text
txtTest1.Clear();
txtTest2.Clear();
txtTest3.Clear();
txtAverage.Clear();
txtLetterGrade.Clear();
// Set Focus
txtTest1.Focus();
}
private void btnCalculate_Click(object sender, EventArgs e)
{
// Declare variables
byte bytTest1;
byte bytTest2;
byte bytTest3;
float fltAverage;
string strLetterGrade = "F";
// Convert to text
if (byte.TryParse(txtTest1.Text, out bytTest1))
{
}
else
{
MessageBox.Show("Invalid Number!",
"Yancarlos Grade Calculator",
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
txtTest1.Focus();
return;
}
if (byte.TryParse(txtTest2.Text, out bytTest2))
{
}
else
{
MessageBox.Show("Invalid Number!",
"Yancarlos Grade Calculator",
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
txtTest2.Focus();
return;
}
if (byte.TryParse(txtTest3.Text, out bytTest3))
{
}
else
{
MessageBox.Show("Invalid Number!",
"Yancarlos Grade Calculator",
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
txtTest3.Focus();
return;
}
// Formula
fltAverage = (bytTest1 + bytTest2 + bytTest3) / 3;
if (fltAverage <= 59.9)
{
strLetterGrade = "F";
}
else if (fltAverage <= 63.9)
{
strLetterGrade = "D-";
}
else if (fltAverage <= 66.9)
{
strLetterGrade = "D";
}
else if (fltAverage <= 69.9)
{
strLetterGrade = "D+";
}
else if (fltAverage <= 73.9)
{
strLetterGrade = "C-";
}
else if (fltAverage <= 76.9)
{
strLetterGrade = "C";
}
else if (fltAverage <= 79.9)
{
strLetterGrade = "C+";
}
else if (fltAverage <= 83.9)
{
strLetterGrade = "B-";
}
else if (fltAverage <= 86.9)
{
strLetterGrade = "B";
}
else if (fltAverage <= 89.9)
{
strLetterGrade = "B+";
}
else if (fltAverage <= 93.9)
{
strLetterGrade = "A-";
}
else if (fltAverage <= 96.9)
{
strLetterGrade = "A";
}
else
{
strLetterGrade = "A+";
}
// convert back to user
txtAverage.Text = fltAverage.ToString("f1");
txtLetterGrade.Text = strLetterGrade.ToString();
}
private void btnExit_Click(object sender, EventArgs e)
{
// Exit application
Application.Exit();
}
private void frmGradeCalculator_Load(object sender, EventArgs e)
{
}
}
}
答案 0 :(得分:1)
当你计算平均值时,给它一个镜头:
double totalScore = bytTest1 + bytTest2 + bytTest3;
double lowest = Math.Min(Math.Min(bytTest1, bytTest2), bytTest3);
if (chkDropLowest.IsChecked == true)
{
// Drop the lowest test
fltAverage = (totalScore - lowest) / 2;
}
else
{
// Include all three tests
fltAverage = (totalScore) / 3;
}
答案 1 :(得分:1)
要回答有关是否使用循环或if语句的特定问题,所有复选框控件都有一个可以检查的Checked属性。
if(chkDropLowest.Checked)
{
//Calculate average without the lowest test
}
就设计而言,我建议您不要为特定按钮分配如此多的功能。我会创建一个从该按钮调用的新方法(如果您决定将来使用新控件,那么您将是安全的)
示例:
private string CalculateLetterGrade(byte test1, byte test2, byte test3)
{
string letterGrade;
if(chkDropLowest.Checked)
{
//Drop lowest test then calculate average
}
else
{
//Use all tests then calculate average
}
//Determine the string for the letter grade, then return it
return letterGrade;
}
你似乎已经找到了平均逻辑,所以我会把它留给你。
答案 2 :(得分:0)
为了感兴趣,我已经重构了你的btnCalculate_Click
。这解决了您的问题,但我已经发布了更多信息,以便您可以查看更简洁地解决您的问题的代码,并且可能会给您一些语法来查看。
private void btnCalculate_Click(object sender, EventArgs e)
{
Func<TextBox, double?> parseTextBox = tb =>
{
double value;
if (!double.TryParse(tb.Text, out value))
{
MessageBox.Show("Invalid Number!",
"Yancarlos Grade Calculator",
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
tb.Focus();
return null;
}
return value;
};
double?[] tests =
(
from tb in new[] { txtTest3, txtTest2, txtTest1, }
let result = parseTextBox(tb)
orderby result descending
select result
).ToArray();
if (tests.Any(t => t == null))
{
return;
}
double average =
tests
.Take(3 - (checkBox1.Checked ? 1 : 0))
.Average()
.Value;
var grades = new[]
{
new { Score = 59.9, Grade = "F" },
new { Score = 63.9, Grade = "D-" },
new { Score = 66.9, Grade = "D" },
new { Score = 69.9, Grade = "D+" },
new { Score = 73.9, Grade = "C-" },
new { Score = 76.9, Grade = "C" },
new { Score = 79.9, Grade = "C+" },
new { Score = 83.9, Grade = "B-" },
new { Score = 86.9, Grade = "B" },
new { Score = 89.9, Grade = "B+" },
new { Score = 93.9, Grade = "A-" },
new { Score = 96.9, Grade = "A" },
new { Score = 100.0, Grade = "A+" },
};
string grade =
grades
.Where(g => g.Score >= average)
.Select(g => g.Grade)
.First();
txtAverage.Text = average.ToString("f1");
txtLetterGrade.Text = grade;
}