简体   繁体   中英

How would I decrease the count in the lblTotalCredits, lblNumberCourses, lblCurrentGPA. Also, how would I calculate GPA?

Here is what I have so far as well as included comments to sort of convey my thought process to you all.

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";
}

}

I am trying to build the code so it reduces the count when I remove items from the list just like when I increased the count when I added items. I also need some assistance with setting up and calculating the GPA

I'd make a new var called lblTotalCreditsInt or something, then set lblTotalCredits.text to lblTotalCreditsInt.ToString(). same thing with the other values. GPA is calculated like this https://gpacalculator.net/how-to-calculate-gpa/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM