【发布时间】:2017-11-11 17:32:33
【问题描述】:
因此,对于我们的 c# 类,我们必须创建一个包含 5 个对象的数组,这些对象具有 4 个属性
工作编号的整数
作业名称的字符串
职位描述字符串
预期工作时间的两倍。
我需要在我的循环中防止用户输入重复的工作编号:这是我目前的代码。此代码有效,但允许重复的工作编号;
namespace JobDemo2
{
class Program
{
static void Main(string[] args)
{
Job[] jobbies = new Job[5];
int x;
int jobNum;
string customerName;
string description;
double hours;
const double RATE = 45.00;
for (x = 0; x < jobbies.Length; ++x)// creates array
{
GetJobData(out jobNum, out customerName, out description, out hours, jobbies);
jobbies[x] = new Job(jobNum, customerName, description, hours);
}
//Array.Sort(jobbies);
Console.WriteLine("The jobs, sorted, are: ");
for (x = 0; x < jobbies.Length; ++x) // prints the array values
{
DisplayJobs(jobbies[x]);
}
double totalRevenue = (jobbies[0].Hours + jobbies[1].Hours +
jobbies[2].Hours + jobbies[3].Hours + jobbies[4].Hours) * RATE;
Console.WriteLine();
Console.WriteLine("The total revenue projected is {0}", totalRevenue);
Console.ReadKey();
}
static void GetJobData(out int jobNum,
out string customerName, out string description, out double hours,
Job[] jobbies)
{
string inString;
Console.Write("Please enter a job number >> ");
inString = Console.ReadLine();
int.TryParse(inString, out jobNum);
Console.Write("Please enter the customer's name for this job >> ");
customerName = Console.ReadLine();
Console.Write("Please enter the job's description >> ");
description = Console.ReadLine();
Console.Write("Please enter the projected hours for the job >> ");
inString = Console.ReadLine();
double.TryParse(inString, out hours);
Console.WriteLine();
}
static void DisplayJobs(Job jobbies)
{
Console.WriteLine("{0, 5} {1, -10} {2, 6} {3, 8}",
jobbies.JobNumber, jobbies.Customer, jobbies.Description, jobbies.Hours);
}
}
class Job //object
{
private double hours;
private double price;
public const double RATE = 45.00;
public Job(int num, string cust, string desc, double hrs)
{
JobNumber = num;
Customer = cust;
Description = desc;
Hours = hrs;
}
public int JobNumber { get; set; }
public string Customer { get; set; }
public string Description { get; set; }
public double Hours
{
get
{
return hours;
}
set
{
hours = value;
price = hours * RATE;
}
}
public double Price
{
get
{
return price;
}
}
public override string ToString()
{
return (GetType() + " " + JobNumber + " " + Customer + " " +
Description + " " + Hours + " hours @" + RATE.ToString("C") +
" per hour. Total price is " + Price.ToString("C"));
}
public override bool Equals(Object e)
{
bool equal;
Job temp = (Job)e;
if (JobNumber == temp.JobNumber)
equal = true;
else
equal = false;
return equal;
}
public override int GetHashCode()
{
return JobNumber;
}
}
}
老师向全班建议我们在此处添加另一个用于比较对象的 for 循环。那个 for 循环会是什么样子?
这是她的电子邮件:
帮助循环创建一个布尔变量。
for 循环遍历您的数组以要求用户输入信息并将您的 bool 变量设置为 true。内部的另一个for循环调用类中的equals方法,将刚刚输入的作业与数组中的每个对象进行比较。这是大多数人搞砸的地方,因为您必须将对象与对象进行比较,而不是将作业编号的整数与整个对象进行比较。如果对象相等,则将 bool 设置为 false。
当 bool 为 false 时,您想告诉他们输入了错误的数字并再次输入。在此处将 bool 设置为 true,然后进行相同的 for 循环再次进行比较。只要数字保持为假,用户就会被困在这个while循环中。当他们输入正确的数字时,它就会爆发。
【问题讨论】:
-
到目前为止你尝试过什么?调用
Equals时遇到什么错误? -
所以,我尝试创建一个看起来像这样的 for 循环;
for (x=0; x -
你为什么不用字典。如果需要,您可以转换为数组。
-
因为这超出了项目的范围。这是我在 C# 的第一个学期。
-
首先:请在您的问题中也进行编辑。第二:您是否将覆盖的
Equals放在Job类中?第三:Equals比较 2 个对象,在您的情况下为Job类型。所以你必须把它叫做bool res=myJob.Equals(otherJob)。