我无法弄清楚为什么我的代码不会打印GPA的计算

时间:2016-02-26 14:31:39

标签: c# visual-studio-2012

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);

        }
    }
}

----这最后一部分是我认为是错误的,但我不知道如何解决它。它不会输出实际计算的gpa

1 个答案:

答案 0 :(得分:2)

你需要这一行 -

Console.Write("Your GPA is " + gpa);

或者

Console.Write("Your GPA is {0}" ,gpa);