我一直在做这个家庭作业一段时间我现在有2个问题远没有完成......这个问题尤其令我难以理解,我甚至不确定我明白它的要求......它已经晚了,咖啡已经消失了有人能指出我正确的方向......
**对于List letterGrades中的所有元素如果索引i处的元素等于grade,则将noOfGrades增加1。 无论情况如何,都应该进行比较,即p / P / f / F.
这是我目前的代码列表......
/*
* Calculate letter grades as P for Pass and F for Fail based on the marks obtained.
* Find the number of instances with the given grades.
*/
namespace Ex1
{
public partial class LetterGrade : Form
{
// Initialize string List
public List<string> letterGrades = new List<string>();
public LetterGrade()
{
InitializeComponent();
}
private void submitButton_Click(object sender, EventArgs e)
{
double marks = double.Parse(marksTextbox.Text); //Declare variable marks and set
// If statements for marks
if (marks>0 && marks<=60)
{
letterGrades.Add("F");
}
else if (marks>60 && marks<=100)
{
letterGrades.Add("P");
}
else
{
MessageBox.Show("Not a valid marks");
}
marksTextbox.Clear();
}
private void calculateButton_Click(object sender, EventArgs e)
{
string grade = letterGradeTextbox.Text; // initialize string grade
int noOfGrades = 0;
for (int i = 0; i < letterGrades.Count; i++)
{
if (grade == "P" || grade == "p") //Problem here, step 7a...could not initialize using [i]..tried using a workaround but it keeps total count..
{
noOfGrades++;
}
else if (grade == "F" || grade == "f")
{
noOfGrades++;
}
}
letterGradeTextbox.Clear();
MessageBox.Show("Number of instances with given letter grade is: " + noOfGrades);
}
}
}
我很有可能过去想到月球和背部,但是如果有人可以指出我对了正确的兔子洞,那我就非常感激
答案 0 :(得分:1)
试试这个
for (int i = 0; i < letterGrades.Count; i++)
{
if (letterGrades[i].ToLower() == grade.ToLower())
{
noOfGrades++;
}
}
基本上,对于letterGrades
中的每个成绩,我们要检查该成绩是否等于grade
1>}