Как бы я уменьшил количество кредитов в lblTotalCredits, lblNumberCourses, lblCurrentGPA. Кроме того, как бы я рассчитал средний балл?

#c#

Вопрос:

Вот что у меня есть до сих пор, а также включенные комментарии, чтобы как бы передать вам всем мой мыслительный процесс.

 protected void Page_Load(object sender, EventArgs e)
{

}

protected void btnContinue_Click(object sender, EventArgs e)
{
    //takes input from user and stores it appropriately
    lblFirstName.Text = txtFirstName.Text;
    lblLastName.Text = txtLastName.Text;
    lblMajor.Text = ddlMajors.SelectedValue;
    //makes second panel visible and hides original panel
    pnlCourseInfo.Visible = true;
    pnlStudent.Visible = false;
    lblTotalCredits.Text = "0";
    lblNumberCourses.Text = "Unknown";
    lblCurrentGPA.Text = "0.0";
}

protected void lbAddCourse_Click(object sender, EventArgs e)
{
    //make add course panel visible
    pnlCourseAdd.Visible = true;
}

protected void rblAddedCourses_SelectedIndexChanged(object sender, EventArgs e)
{
    //make remove button visible
    btnRemoveSelected.Visible = true;
}

protected void btnSubmit_Click(object sender, EventArgs e)
{
    //make RadioButtonList visible
    rblAddedCourses.Visible = true;



    //create variables for summary items
    int intCourseCount = 0;
    int intTotalCredits = 0;
    decimal decCurrentGPA = 0m;


    //store course entered as variable
    string strNewCourse = txtCoursePrefix.Text.ToUpper()   " "   txtCourseNumber.Text   " "   "("   ddlGradeEarned.SelectedItem   ")";

    //loop thru to make sure duplicate course is not added
    bool blnDuplicateCourse = false;

    foreach (ListItem liCourses in rblAddedCourses.Items)
    {
        //if the current courses matches the new course, set flag to true
        if (liCourses.Text == strNewCourse)
        {
            blnDuplicateCourse = true;
        }
    }
    //only add course if it is a new course
    if (blnDuplicateCourse == true)
    {
        lblError.Text = "This course already exists! Please enter a new course.";
    }
    else
    {
        //new course not in the list, clear error message and add to RadioButtonList
        lblError.Text = "";
        rblAddedCourses.Items.Add(strNewCourse);
    }


    //create increments for number of courses and total credits
    foreach (ListItem liCourse in rblAddedCourses.Items)
    {
        intCourseCount  = 1;
        intTotalCredits  = 3;
    }



    //calculate GPA
    if (ddlGradeEarned.SelectedValue == "A")
    {

    }
    //assign value to summary labels
    if (intCourseCount >= 1)
    {
        lblNumberCourses.Text = intCourseCount.ToString();
    }

    if (decCurrentGPA >= 1)
    {
        lblCurrentGPA.Text = decCurrentGPA.ToString();
    }

    if (intTotalCredits > 0)
    {
        lblTotalCredits.Text = intTotalCredits.ToString();
    }
}


protected void btnRemoveSelected_Click(object sender, EventArgs e)
{
    //remove selected course
    rblAddedCourses.Items.RemoveAt(rblAddedCourses.SelectedIndex);

    //hide button until another selection is made
    btnRemoveSelected.Visible = false;

    int intCourseCount = 0;
    int intTotalCredits = 0;
    decimal decCurrentGPA = 0m;

    foreach (ListItem liCourse in rblAddedCourses.Items)
    {
        intCourseCount -= 1;
        intTotalCredits -= 3;
    }
    
    if (intCourseCount >= 1)
    {
        lblNumberCourses.Text = intCourseCount.ToString();
    }

    if (decCurrentGPA >= 1)
    {
        lblCurrentGPA.Text = decCurrentGPA.ToString();
    }

    if (intTotalCredits > 0)
    {
        lblTotalCredits.Text = intTotalCredits.ToString();
    }
}

protected void btnCancel_Click(object sender, EventArgs e)
{
    //hide add course panel and clear contents
    txtCourseNumber.Text = "";
    txtCoursePrefix.Text = "";
    ddlGradeEarned.SelectedIndex = 0;
    pnlCourseAdd.Visible = false;
}

protected void lbStartOver_Click(object sender, EventArgs e)
{
    //hide panel for course information and clear all contents
    txtFirstName.Text = "";
    txtLastName.Text = "";
    ddlMajors.SelectedIndex = 0;
    ddlGradeEarned.SelectedIndex = 0;
    txtCourseNumber.Text = "";
    txtCoursePrefix.Text = "";
    pnlCourseInfo.Visible = false;
    pnlStudent.Visible = true;
    lblCurrentGPA.Text = "0.0";
    lblNumberCourses.Text = "Unknown";
    lblTotalCredits.Text = "0";
}
 

}

Я пытаюсь построить код таким образом, чтобы он уменьшал количество элементов, когда я удаляю элементы из списка, точно так же, как когда я увеличивал количество при добавлении элементов. Мне также нужна помощь в настройке и расчете среднего балла

Комментарии:

1. Это Winforms?

Ответ №1:

Я бы создал новый var под названием lblTotalCreditsInt или что-то в этом роде, а затем установил lblTotalCredits.text в lblTotalCreditsInt.toString(). то же самое с другими значениями. Средний балл рассчитывается следующим образом https://gpacalculator.net/how-to-calculate-gpa/