首页 > 解决方案 > 为什么我的程序需要两行输入,为什么我的 GPA 计算错误 c#?

问题描述

我正在制作一个计算 GPA 的程序。我大部分时间都在工作,但我不知道为什么它要求 2 个输入以及为什么 GPA 的计算是错误的。我还需要这样做,以便我可以输入大写或小写字母。如果您能提供一种重写的方法,将不胜感激。

这是程序:

using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;

namespace gpa
{
    class Program
    {
        static void Main(string[] args)
        {
            //create a list for the grades to go into
            List<double> GradeList = new List<double>();


            Console.Write("\n********************************************\n");
            bool LastGrade = false;
            while (LastGrade == false)
            {



                //pass letter as parameter to get the GradePoint
                double Grade = Calculations.GetGradePoint();

                if (Console.ReadLine() == "")
                {
                    LastGrade = true;
                }
                else
                {
                    GradeList.Add(Grade);
                }
            }

            //add all the gradepoints and round to 2 decimal places
            double TotalGradePoints = Math.Round(GradeList.Sum(), 2);
            Console.WriteLine($"your total grade points are {TotalGradePoints}");

            Console.WriteLine("How many total credits did you take this semester?");
            double TotalCredits = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine($"total credits {TotalCredits}");

            double EndGPA = Calculations.GPA(TotalGradePoints, TotalCredits);
            Console.WriteLine($"Your GPA this semester will be {EndGPA}");



        }
    }
}

这是我添加的计算类:

using System;
namespace gpa
{
    public class Calculations
    {

        public static double GPA(double points, double credits)
        {
            double average = points / credits;
            return average;

        }

        public static double GetGradePoint()
        {
            Console.WriteLine("Enter your letter grade for each class");
            double Grade = 0;
            string letter = Console.ReadLine();
            if (letter == "A")
            {
                return 4;
            }
            if (letter == "A-")
            {
                return 3.7;
            }
            if (letter == "B+")
            {
                return 3.3;
            }
            if (letter == "B")
            {
                return 3;
            }
            if (letter == "B-")
            {
                return 2.7;
            }
            if (letter == "C+")
            {
                return 2.3;
            }
            if (letter == "C")
            {
                return 2;
            }
            if (letter == "C-")
            {
                return 1.7;
            }
            if (letter == "D+")
            {
                return 1.3;
            }
            if (letter == "D")
            {
                return 1;
            }
            if (letter == "F")
            {
                return 0;
            }
            return Grade;
            //do not need to add looping mechanism in this fucntion - loop it in the main function

            // Write function that takes a letter grade as input
            // and returns the grade-point value of that letter grade

            // Replace your switch statement above with a call to this function

        }




    }
}

这是输出: 在此处输入图像描述

标签: c#functionmethodscalculatorgpa

解决方案


你的程序逻辑有缺陷。所以。您有一个循环不断循环并调用一个方法。在这种方法GetGradePoint中,您要求输入成绩,然后使用 读取输入Console.ReadLine()

这可以。问题随之而来。您再次等待用户的输入,如果他们输入的不是空字符串,程序将添加第一个输入的等级值,然后循环返回,因为LastGrade仍然是错误的。

这意味着程序从循环的开头开始,这意味着它将再次调用该GetGradePoint方法,这就是它再次要求输入的原因。此外,如果您随后输入一个空字符串,则用户输入的成绩不会添加到GradeList列表中。

正如我所说,你需要通过你的程序,因为问题是你的程序的逻辑。我怀疑 GPA 错误的另一个问题也会自行纠正。

代码提示:

而不是执行 1,000 个 if 语句,因为您只接受一个字符,您可以将其else if用于其余语句。尽管这并不重要,因为您正在返回一个值。IE:

if(condition)
{
}
else if(other condition)
{
}

您的 while 循环接受一个布尔值作为==提供的条件,但您可以改为使用您的LastGrade变量作为循环 IE 的条件:

while(!LastPass)
{
}

至于获取输入,我会将其重写为类似

//Remove the line in GetGradePoint if you're gonna put it here
Console.WriteLine("Enter your grades");
while(condition)
{
    string input = Console.ReadLine();
    if(input != "")
    {
        var grade = GetGradePoint(input);
        GradeList.Add(grade);
    }
    else LastGrade = true;
}
//Rest of code to calculate GPA

这是粗略的伪 C# 代码。至于接受小写和大写:.ToUpper()即使用户输入小写字母,您也可以在输入上使用以确保输入是大写字母。


推荐阅读