因此,對於我們的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
爲什麼你不使用字典。如果需要可以轉換爲數組。 –