Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
178 views
in Technique[技术] by (71.8m points)

c# - How do I display grade letters?

I have a program that is suppose to display the corresponding letter grade A,B,C,D,E. Instead is prints the numeric value. I classified it as a decimal because I did not know what you classify text under. How do I fix this?

private void CalculateButton_Click(object sender, EventArgs e)
{
    decimal Score = 0 , Percent, Average = 0, Letter = 0, GrandTotal = 0;

    if (NameTextBox.Text != "" && ScoreTextBox.Text != "")
    {
        if (Score >= 0 && Score <= 150)
        {
            Score = decimal.Parse(ScoreTextBox.Text);
            Percent = Score / 150;
            if (Score >= 90)
                Letter = 'A';
            else if (Score >= 80 && Score < 90)
                Letter = 'B';
            else if (Score >= 70 && Score < 80)
                Letter = 'C';
            else if (Score >= 60 && Score < 70)
                Letter = 'D';
            else if (Score < 60)
                Letter = 'E';

            GrandTotal += 1;
            Average += Score; 

            PercentTextBox.Text = Percent.ToString("P");
            LetterGradeTextBox.Text = Letter.ToString();
            GrandTotalTextBox.Text = Percent.ToString();
            AverageTextBox.Text = Average.ToString("P");
        }
        else
            MessageBox.Show("Score must be a positive number between 0 - 150");
    }
    else
        MessageBox.Show("Name and a Score between 0 - 150 are required");
}
question from:https://stackoverflow.com/questions/65907465/how-do-i-display-grade-letters

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

change Letter to be a string, instead of a decimal:

string Letter = "";

then set your textboxes to the letter:

LetterGradeTextBox.Text = Letter;

Also, you'll need to change setting Letter to a string (double quotes):

 Letter = "A";

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...