0

私は現在 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();
    }
}
}

ご協力いただきありがとうございます!

4

3 に答える 3

1

データ型を整数から倍精度に変更します。

つまり、次のように変更します

    int pID;
    int cR;
    int hR;
    int eR;

    double pID;
    double cR;
    double hR;
    double eR;

分子が分母より小さい場合、整数除算は 0 になります。

FormatException については、正しい数字を入力していることを確認する必要があります。文字を入力している場合、この例外が発生する可能性があります。

(FormatException の代わりに) 解析エラーの場合に false を返すint.TryParseを使用できます。これにより、間違った入力をより適切に処理できます。

于 2013-10-05T02:31:25.967 に答える
0

間違ったコンストラクター関数を呼び出しているため、常にゼロを返します。前の 2 つの提案に従って変更してください。

static void Main( )
{
    ProfessorRating professorObject = new ProfessorRating();

    int pID;
    int cR;
    int hR;
    int eR;

ProfessorRating professorObject = new ProfessorRating();変数の下に移動し、professorObject を変更して適切なメソッドを呼び出すことにより、コンストラクター メソッドを記述します。

pID = DataEntry("Enter the ID of the Professor you would like to rate");
        cR = DataEntry("Please rate the clearity of the information provided");
        hR = DataEntry("Please rate your instructor on their helpfulness");
        eR = DataEntry("Please rate your material on its easiness");

        ProfessorRating professorObject = new ProfessorRating(pID, cR, hR, eR);
        professorObject.GradeAverage();
于 2014-04-21T04:23:27.150 に答える
0

この回答は、@ Tilak sir's Answer に追加したコメントに関するものです。

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
}

そのため、「すべてのコードパスが値を返すわけではない」エラーを削除するために次の行を追加します。

    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
Return Rating;
    }

DataEntry は int Method であり、何らかの値を返す必要があるため、「すべてのコードパスが値を返すわけではありません」エラーが発生しています...

それがあなたに役立つことを願っています....

于 2013-10-05T03:55:46.937 に答える