C# 为什么我的程序需要两行输入,为什么我的GPA计算错误?
我正在做一个计算平均成绩的程序。我让它大部分工作,但我不明白为什么它要求2个输入,为什么GPA的计算是错误的。我还需要使它,以便我可以输入大写或小写字母。如果您能提供一种重写的方法,我们将不胜感激 这是程序:C# 为什么我的程序需要两行输入,为什么我的GPA计算错误?,c#,function,methods,calculator,gpa,C#,Function,Methods,Calculator,Gpa,我正在做一个计算平均成绩的程序。我让它大部分工作,但我不明白为什么它要求2个输入,为什么GPA的计算是错误的。我还需要使它,以便我可以输入大写或小写字母。如果您能提供一种重写的方法,我们将不胜感激 这是程序: using System; using System.IO; using System.Linq; using System.Collections.Generic; namespace gpa { class Program { static void
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
}
}
}
这是输出:
您的程序逻辑有缺陷。所以您有一个保持循环并调用方法的循环。在这个方法中,
GetGradePoint
您请求成绩输入,然后使用Console.ReadLine()
读取输入
这很好。问题随之而来。您再次等待用户的输入,如果他们输入的不是空字符串,程序将添加第一个输入等级值,然后循环返回,因为LastGrade
仍然为false
这意味着程序从循环的开头开始,这意味着它将再次调用GetGradePoint
方法,这就是它再次请求输入的原因。此外,如果随后输入空字符串,则用户输入的成绩不会添加到成绩列表
列表中
你需要通过你的程序,因为问题,正如我所说的,是你程序的逻辑。我怀疑GPA错误的另一个问题也会自我纠正
代码提示:
不要执行1000条if语句,因为您只接受一个字符,所以可以对其余语句使用else if
。虽然这并不重要,因为您正在返回一个值。即:
if(条件)
{
}
else if(其他条件)
{
}
while循环接受一个布尔值作为=
提供的条件,但您可以使用LastGrade
变量作为循环的条件,即:
while(!LastPass)
{
}
至于获取输入,我会将其改写为
//如果要放在这里,请删除GetGradePoint中的行
Console.WriteLine(“输入您的成绩”);
while(条件)
{
字符串输入=Console.ReadLine();
如果(输入!=“”)
{
var等级=GetGradePoint(输入);
成绩表。添加(成绩);
}
else LastGrade=真;
}
//计算GPA的其余代码
这是粗略的伪c代码。
至于接受小写和大写:您可以对输入使用.ToUpper()
,以确保即使用户输入小写字母,输入也是大写字母。Console.ReadLine()可以包含一个换行,而不是空字符串。每个ReadLine都从控制台读取一个新行