因此,对于我们的C#类中,我们必须创建5个对象的数组,有4个属性循环用于防止重复输入到一个数组
作业数量
作业名
一个字符串一个int工作描述的字符串
工作时间的两倍。
我需要在我的循环中防止用户输入重复的工作号码:这是我的代码到目前为止。此代码有效,但会允许复制作业编号;
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循环是什么样的?
这里是她的电子邮件:
帮助的循环创建一个布尔变量。
用于循环访问数组,以要求用户输入信息并将bool变量设置为true。另一个for循环内部调用类中的equals方法,该方法将比较刚刚输入到数组中的每个对象的作业。这是大多数人搞乱的地方,因为你必须将对象与对象进行比较,而不是将作业号码的整数与整个对象进行比较。如果对象相等则将bool设置为false。
而布尔是假的,你想告诉他们他们输入了错误的数字并再次输入。在这里将bool设置为true,然后再次循环进行比较。只要数字保持不变,用户就会陷入这个while循环中。当他们输入一个正确的号码时,它会爆发。
你到目前为止尝试过什么?调用'Equals'时会出现什么错误? – casiosmu
所以,我试图创建一个for循环,看起来像这样;
for(x = 0; x <2; ++ x) Equals(); } 我知道这是错误的,而不是x <2,我不知道它应该是什么(循环直到输入非重复),以及需要传递给Equals方法的是什么?我不确定这里的语法。 – James
为什么你不使用字典。如果需要可以转换为数组。 –