c#让用户从数组中的元素中进行选择
我有一个项目,在这个项目中,用户输入工作描述、所需小时数和5份工作的小时工资。一旦输入,他们必须从输入的作业中选择两个合并为一个作业。我不确定如何从数组中检索他们输入的作业编号(jobArray)(例如作业1和作业2) 因此,如果他们在作业1中输入割草,作业2中输入修剪,他们应该可以选择将两者结合起来进行割草和修剪 这是我的代码,任何帮助都将不胜感激c#让用户从数组中的元素中进行选择,c#,arrays,C#,Arrays,我有一个项目,在这个项目中,用户输入工作描述、所需小时数和5份工作的小时工资。一旦输入,他们必须从输入的作业中选择两个合并为一个作业。我不确定如何从数组中检索他们输入的作业编号(jobArray)(例如作业1和作业2) 因此,如果他们在作业1中输入割草,作业2中输入修剪,他们应该可以选择将两者结合起来进行割草和修剪 这是我的代码,任何帮助都将不胜感激 namespace DemoJobs { class Class2 { public static
namespace DemoJobs
{
class Class2
{
public static Job[] jobArray = new Job[5];
static void Main(string[] args)
{
string option;
do
{
Console.WriteLine("Menu");
Console.WriteLine("\t1. Enter Jobs");
Console.WriteLine("\t2. Combine 2 Jobs");
Console.WriteLine("\t3. Display All Jobs");
Console.WriteLine("\t4. Exit");
option = Console.ReadLine();
switch (option)
{
case "1":
Console.Clear();
EnterJobs();
break;
case "2":
Console.Clear();
//CombineJobs();
break;
case "3": //display jobs
DisplayAllJobs();
break;
case "4":
break;
default:
Console.WriteLine("Option invalid.");
Console.ReadLine();
break;
}
} while (option != "4");
Console.WriteLine("Press enter to exit the window.");
Console.ReadLine();
}
private static void EnterJobs()
{
for (int i = 0; i < jobArray.Length; i++)
{
// int totFee;
Job job = new Job();
Console.WriteLine("Job " + i);
Console.WriteLine("Enter a job description.");
job.Description = Console.ReadLine();
Console.WriteLine("Enter the amount of hours required to complete the job.");
job.hoursToComplete = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the hourly rate for the job.");
job.hourlyRate = Convert.ToDouble(Console.ReadLine());
jobArray[i] = job;
//calcing total fee
job.totalFee = job.hourlyRate * job.hoursToComplete;
}
Console.WriteLine(" ");
} //end of enterjobs
//combining jobs
private static void CombineJobs(Job first, Job second)
{
Console.WriteLine("Which 2 jobs would you like to combine?");
first.Description = Console.ReadLine();
}
private static void DisplayAllJobs()
{
// jobArray.ToList().Sort();
//sorting jobs from totalFee
Array.Sort(jobArray);
//printing array
Console.WriteLine(jobArray[0].ToString());
Console.WriteLine(jobArray[1].ToString());
Console.WriteLine(jobArray[2].ToString());
Console.WriteLine(jobArray[3].ToString());
Console.WriteLine(jobArray[4].ToString());
}
}
}
名称空间DemoJobs
{
二级
{
公共静态作业[]作业数组=新作业[5];
静态void Main(字符串[]参数)
{
字符串选项;
做
{
Console.WriteLine(“菜单”);
Console.WriteLine(“\t1.Enter Jobs”);
Console.WriteLine(“\t2.combine2作业”);
Console.WriteLine(“\t3.Display All Jobs”);
Console.WriteLine(“\t4.Exit”);
option=Console.ReadLine();
开关(选件)
{
案例“1”:
Console.Clear();
EnterJobs();
打破
案例“2”:
Console.Clear();
//CombineJobs();
打破
案例“3”://显示作业
显示所有作业();
打破
案例“4”:
打破
违约:
Console.WriteLine(“选项无效”);
Console.ReadLine();
打破
}
}而(选项!=“4”);
Console.WriteLine(“按enter键退出窗口”);
Console.ReadLine();
}
私有静态void EnterJobs()
{
for(int i=0;i
您可以像这样搜索数组
Job jobToFind = jobArray.FirstOrDefault(s => s.Description.Equals(first.Description));
Job job2ToFind = jobArray.FirstOrDefault(s => s.Description.Equals(second.Description));
然后根据您的需要合并工作
newJob.Description = jobToFind.Description + job2ToFind.Description;
// newJob.Pay = more combining here etc.
这个问题真是一团糟,我甚至不知道CombineJobs()的意义是什么,为什么它会改变第一个工作描述?解释“合并作业”的实际含义,并帮助您实现。是的,您应该创建一个局部变量来保存输入读线,然后首先检查它。说明。我会打印作业列表及其索引(或其他ID),并要求用户选择第一个ID,然后让他们输入第二个id。然后您可以从数组中获取这些id并进行合并。@当用户按菜单上的2以提示用户选择要合并的两个作业(标题、工时和工资)时,将执行downrep_nation combinejobs,我的意思是删除代码的第一行。描述。