0

我目前正在学习 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();
    }
}
}

谢谢你的帮助!

4

3 回答 3

1

将数据类型从整数更改为双精度。

即,更改以下

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

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

当分子小于分母时,整数除法导致 0。

对于 FormatException,您必须确保输入正确的数字。如果您正在输入字符,您很可能会遇到此异常。

您可以使用int.TryParse在解析错误的情况下返回 false(而不是 FormatException)。这样您可以更好地处理错误输入。

于 2013-10-05T02:31:25.967 回答
0

您将始终返回零,因为您调用了错误的构造函数请按照前两个建议进行操作,然后进行更改

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 先生的回答中添加的评论。

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 方法,它应该返回一些值...

希望它对你有用....

于 2013-10-05T03:55:46.937 回答