私は現在 C# を学んでおり、課題に少し行き詰まりました。私は Visual Studio 2010 を使用しており、私の割り当ては次のとおりです。
教授 ID と 3 つの評価から構成される ProfessorRating クラスを作成するプログラムを作成します。3 つの評価は、使いやすさ、有用性、わかりやすさを評価するために使用されます。別の実装クラスで、ユーザーが値を入力できるようにします。コンストラクターを呼び出して、ProfessorRating クラスのインスタンスを作成します。適切なプロパティを含めます。オブジェクトの作成後に ID を変更できないようにしてください。全体の評価平均を計算して返すために、ProfessorRating クラスにメソッドを提供します。実装クラスから、すべての評価と、小数点の右側に数字なしで書式設定された平均評価を出力します。単一のクラス メソッドを使用して、すべてのデータを入力します。
これまでのところ、私はほとんどの課題をこなしてきましたが、最終的に自分では見つけられない 2 つのバグに遭遇しました。
コードは 2 つのファイルにあり、これが最初のファイルです。
//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
}
}
}
2 番目のファイルのコードは次のとおりです。
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();
}
}
}
ご協力いただきありがとうございます!