删除列表框中的项目时更新的GPA

时间:2018-09-29 03:15:01

标签: c# winforms checkbox radio-button listboxitem

我创建了一个列表框,其中显示了已选中的成绩,并且所选的学分加上相应的GPA。列表框项目的每一行都会计算累积GPA。例如,列表框显示:

  1. A-1 4.000
  2. C-1 3.000
  3. D-3 1.800

如果我删除最后一项,则GPA累计将为3.000。但是,如果我删除第二项,则GPA应该为2.500。但是,当我测试代码时,情况并非如此。

这些是我使用的代码:

//global declarations
List<double> allGrades = new List<double>();
        List<double> allHours = new List<double>();
        List<double> allGPA = new List<double>();
        List<int> gradeValue = new List<int>();
        private RadioButton currentRadioButton;
        Dictionary<char, double> gradeValuesMap;

        public string StudentName { get; set; }

 public DataEntry()//switch back to pass value: string input
        {
            InitializeComponent();
            gradeValuesMap = new Dictionary<char, double>();
            gradeValuesMap.Add('A', 4.0);
            gradeValuesMap.Add('B', 3.0);
            gradeValuesMap.Add('C', 2.0);
            gradeValuesMap.Add('D', 1.0);
            gradeValuesMap.Add('F', 0.0);            
        }


    enter code here

   private void btnEnter_Click(object sender, EventArgs e)
            {
                /*if grades and credits are not selected, Error Message:
                    * MessageBox.Show("A grade must be selected.");
                    * MessageBox.Show("Credit hours must be selected.");
                */
                if (!(radA.Checked || radB.Checked || radC.Checked ||
                    radD.Checked || radF.Checked))
                {
                    MessageBox.Show("A grade must be selected.");
                }

                if (cboCreditHrs.SelectedIndex == -1)//if" no credit is selected
                {
                    MessageBox.Show("Credit hours must be selected.");
                }
                else
                {
                    // do gpa math here
                    // sum(grades * credits) / sum(credits)


              var grade = getGradeValue(currentRadioButton.Text);
              var credit = Convert.ToDouble(cboCreditHrs.SelectedItem.ToString());


                    // sum(4 * 4) / sum(4)
                    var result = (grade * credit) / credit; //GPA calculation

                    allGrades.Add(grade);
                    allHours.Add(credit);

                    double sum = 0;

                    for (int i = 0; i < allGrades.Count; i++)
                    {
                        sum += allGrades[i] * allHours[i];
                    }


                    result = (sum)/ allHours.Sum();

                    string listboxItem = string.Format("{0}-{1} {2}", currentRadioButton.Text, cboCreditHrs.SelectedItem, result.ToString("n3"));
                    lstDetail.Items.Add(listboxItem);

                    // uncheck the currently checked radio button
                    currentRadioButton.Checked = false;

                    // reset the combox selection
                    cboCreditHrs.SelectedIndex = -1;
                }
            }

To remove the selected item. I used the following Even Handler:



     private void btnRmvSelect_Click(object sender, EventArgs e)
        {
            ///*foreach (DataEntry i in lstDetail.*/SelectedItems)
                //lstDetail.Items.Remove(i);

            for (int i = 0; i < lstDetail.SelectedItems.Count; i++)
                lstDetailhttps://stackoverflow.com/questions/20277193/how-to-change-the-selecteditem-foreground-text-of-listbox-item.Items.Remove(lstDetail.SelectedItems[i]);

        }

最有可能的是,我上面的代码缺少某些内容。因此,为什么预期结果不正确。谁能纠正我的代码?

0 个答案:

没有答案