阅读背景:

如果用户不小心输入了一个字母B C D或F以外的字母,我就无法找到一条出错信息。

来源:互联网 
using System;

    namespace gpa
    {
        class gpa
        {
            static void Main(string [] args)
            {
                double credit = 0;
                double totalCreditHours = 0;

            char grade = ' ';
            double gradePoints = 0;
            double totalGradePoints = 0;

            int counter = 0;
            double gpa = 0;

            do 
            {
                Console.Write("Enter letter grade for class #{0} \n(use A, B, C, or D. Type 0 after all classes entered.): ", counter += 1);
                char userInput = char.Parse(Console.ReadLine());

                if (userInput == '0')
                {
                    break;
                }

                else 
                {
                    grade = userInput;
                    Console.Write("Enter your credit hours: ");
                    credit = int.Parse(Console.ReadLine());

                    switch (grade)
                    {
                        case 'A': gradePoints = 4;
                            break;
                        case 'B': gradePoints = 3;
                            break;
                        case 'C': gradePoints = 2;
                            break;
                        case 'D': gradePoints = 1;
                            break;

                    }

                    totalGradePoints = totalGradePoints + (credit * gradePoints);
                    totalCreditHours = totalCreditHours + credit;

                } 

            } while (grade != 0);

                gpa = CalculateGPA(totalGradePoints, totalCreditHours);
                Console.Write("Your GPA is ", gpa);

                Console.ReadKey();
            }


            static double CalculateGPA(double totalGradePoints, double totalCreditHours)
            {
                return (totalGradePoints / totalCreditHours);

            }
        }
    }
using System;

    namespace gpa
    {
        



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: