“FormatException is unhandled”,程序不计算结果
本文关键字:程序 计算 结果 FormatException is unhandled | 更新日期: 2023-09-27 18:33:56
我目前正在学习C#,我有点卡在作业上。我正在使用Visual Studio 2010,我的任务如下:
编写一个程序,创建一个由教授 ID 和三个评级组成的教授评级类。这三个评级用于评估易用性、有用性和清晰度。在单独的实现类中,允许用户输入值。调用构造函数以创建 ProfessorRating 类的实例。包括适当的属性。不允许在构造对象后更改 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();
}
}
}
感谢您的帮助!
将数据类型从整数更改为双精度。
即,更改以下内容
int pID;
int cR;
int hR;
int eR;
自
double pID;
double cR;
double hR;
double eR;
当分子小于分母时,整数除法会导致 0。
对于格式例外,您必须确保输入正确的数字。如果要输入字符,则可能会收到此异常。
您可以使用 int。TryParse,在解析错误的情况下返回false(而不是FormatException(。这样,您可以更好地处理错误的输入。
这个答案是关于你的评论,即你添加@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 方法,它应该返回一些值......
希望它对你有用。
您将始终返回Zero,因为您调用了错误的构造函数,请按照前两个建议进行操作,然后更改
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();