-1
我是一个仍然是C#的新手程序员。我想知道为什么我的应用程序不能正确计算用户的GPA。代码对我来说看起来很好。我很感激,如果有提示也可以改善我的代码。C#简单的控制台应用程序来计算GPA(帮助)
例如,如果一个用户输入级一种用于模块#1的信用单元5,和B级为模块#2的信用单元2中,按0〜退出后,GPA计算将是0.0
基本上用户可以为他想要的任意数量的模块计算他的GPA。模块和学分单元的算法似乎没问题,但我仍然不明白为什么GPA不正确。
class Program
{
static void Main(string[] args)
{
Console.Write("\n********************************************\n");
int creditUnit = 0;
int totalCreditUnit = 0;
char grade = ' ';
int gradePoint = 0;
int total = 0;
int counter = 0;
double GPA = 0;
do
{
Console.Write("Enter grade for module #{0} (press 0 to exit): ", counter += 1);
// write some code to prevent user from entering more than 10
char userInput = char.Parse(Console.ReadLine());
if (userInput == '0')
{
break;
}
else
{
grade = userInput; //assigns value for userInput into gradePoint
Console.Write("Enter credit unit(s) for grade: ");
totalCreditUnit = int.Parse(Console.ReadLine());
switch (grade)
{
case 'A': gradePoint += 4;
break;
case 'B': gradePoint += 3;
break;
case 'C': gradePoint += 2;
break;
case 'D': gradePoint += 1;
break;
}
total += creditUnit * gradePoint;
totalCreditUnit += creditUnit;
}
} while (grade != 0);
GPA = total/totalCreditUnit;
Console.Write("Your GPA this semester is {0:F2}", GPA);
Console.ReadKey();
}
}
谢谢。另外,我意识到,当你将一个int分配给一个int并将其分配给一个double时,结果将被转换为一个int,导致我的值不正确。 – smittenspewpew 2014-11-04 16:35:39