【问题标题】:Why is my program requiring two lines of input and why is my GPA calculation wrong c#?为什么我的程序需要两行输入,为什么我的 GPA 计算错误 c#?
【发布时间】:2020-08-10 07:00:07
【问题描述】:

我正在制作一个计算 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

        }




    }
}

这是输出: enter image description here

【问题讨论】:

  • Console.ReadLine() 可能包含换行符,而不是空字符串。
  • 每个 ReadLine 从控制台读取 new 行

标签: c# function methods calculator gpa


【解决方案1】:

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

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

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

您需要检查您的程序,因为正如我所说,问题在于您的程序的逻辑。我怀疑 GPA 错误的其他问题也会自行纠正。

代码提示:

您可以使用else if 来代替其他语句,而不是执行 1,000 个 if 语句,因为您只接受一个字符。尽管这并不重要,因为您正在返回一个值。即:

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(),以确保输入是大写字母,即使用户输入的是小写字母。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-13
    • 2020-10-05
    • 2016-09-23
    • 1970-01-01
    • 2012-05-21
    • 1970-01-01
    相关资源
    最近更新 更多