我目前正在学习 C#,但我在作业上有点卡住了。我使用的是 Visual Studio 2010,我的作业如下:
编写一个程序,创建一个由教授 ID 和三个评级组成的 ProfessorRating 类。这三个等级用于评估易用性、有用性和清晰性。在单独的实现类中,允许用户输入值。调用构造函数来创建 ProferRating 类的实例。包括适当的属性。构造对象后不允许更改 ID。在 ProfessorRating 类中提供一个方法来计算并返回总体评分平均值。打印实现类中小数点右侧没有数字的所有评级和平均评级。使用单一类方法输入所有数据。
到目前为止,我已经完成了大部分任务,但是我最终遇到了两个我自己无法解决的错误。
代码在两个文件中,这是第一个:
//ProfessorApplication.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Lesson4_v4
{
class ProfessorApplication
{
static void Main( )
{
ProfessorRating professorObject = new ProfessorRating();
int pID;
int cR;
int hR;
int eR;
DisplayInstructions(); //Show instructions
pID = DataEntry("Professor ID of the professor you'd like to rate");
cR = DataEntry("clarity rating of the professor");
hR = DataEntry("helpfulness rating of the professor");
eR = DataEntry("easiness rating of the professor");
professorObject.GradeAverage();
Console.Clear();
Console.Write(professorObject);
Console.ReadLine();
}
static void DisplayInstructions()
{
Console.Beep();
Console.WriteLine("Hello! This program will allow you to calculate a Professor's rating based on three categories!");
Console.WriteLine(" ");
Console.WriteLine("You will be asked for the the professor ID and a grade of 1-10.");
Console.WriteLine(" ");
Console.WriteLine("Please refer to the scale below.");
Console.WriteLine(" ");
Console.WriteLine(" 0 1 2 3 4 5 6 7 8 9 10 ");
Console.WriteLine("Worst Best");
Console.Read();
}
static int DataEntry(string rating1)
{
string inputValue;
int Rating;
Console.WriteLine("Please enter the {0}: ",rating1);
inputValue = Console.ReadLine();
Rating = int.Parse(inputValue); //System.FormatException was unhandled according to VS2010
}
}
}
这是第二个文件中的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Lesson4_v4
{
class ProfessorRating
{
private int professorID; // Professor ID
int helpRating; // Helpfulness Rating
int cleaRating; // Clarity Rating
int easyRating; // Easiness Rating
public ProfessorRating() //default constructor
{
}
public ProfessorRating(int pID) //1 parameter constructor
{
professorID = pID;
}
public ProfessorRating(int pID, int hR, int cR, int eR) //all parameter constructor
{
professorID = pID;
helpRating = hR;
cleaRating = cR;
easyRating = eR;
}
public int GradeAverage() //is supposed to calculate the average, but shows as "0"
{
return (helpRating + cleaRating + easyRating) / 3;
}
public override string ToString()
{
return "The average is " + GradeAverage();
}
}
}
谢谢你的帮助!