C#循环录入4名员工信息并按销售额排序实现咨询
Hey there! No worries about the code structure—everyone starts learning by experimenting, and your single-worker implementation is a solid starting point. Let's fix the loop issue and add the sorting functionality you need.
Step 1: Create a Worker Class to Organize Data
First, let's wrap all worker details into a class. This makes it way easier to store multiple workers and sort them later.
// Define a class to hold all worker information public class Worker { public string Name { get; set; } public int Age { get; set; } public string District { get; set; } public int Sales { get; set; } public int MonthlyPay { get; set; } // We'll calculate this later }
Step 2: Use a Loop to Enter 4 Workers' Data
We'll use a for loop to run 4 times, collecting data each time and adding the worker to a list.
// Create a list to store all workers List<Worker> workers = new List<Worker>(); int basePay = 1000; for (int i = 0; i < 4; i++) { Console.WriteLine($"--- Enter details for Worker {i+1} ---"); Worker newWorker = new Worker(); Console.Write("What is the first- and lastname of the worker: "); newWorker.Name = Console.ReadLine(); Console.Write("What is the age of the worker: "); // Use int.TryParse to avoid errors if user enters non-numeric values (better practice!) int.TryParse(Console.ReadLine(), out newWorker.Age); Console.Write("What district does the worker work at: "); newWorker.District = Console.ReadLine(); Console.Write("How many sales has the worker done: "); int.TryParse(Console.ReadLine(), out newWorker.Sales); // Calculate monthly pay based on sales newWorker.MonthlyPay = basePay; if (newWorker.Sales > 199) { newWorker.MonthlyPay += newWorker.Sales * 4; } else if (newWorker.Sales >= 100) { newWorker.MonthlyPay += newWorker.Sales * 3; } else if (newWorker.Sales >= 50) { newWorker.MonthlyPay += newWorker.Sales * 2; } else { newWorker.MonthlyPay += newWorker.Sales; } // Add the new worker to our list workers.Add(newWorker); Console.WriteLine("\n"); }
Step 3: Sort Workers by Sales
Now we'll sort the list of workers by their sales count. We can use LINQ's OrderByDescending to get highest sales first (or OrderBy for lowest first).
// Sort workers from highest sales to lowest var sortedWorkers = workers.OrderByDescending(w => w.Sales).ToList();
Step 4: Display the Sorted Results
Finally, let's print out all sorted workers with their details and pay:
Console.WriteLine("--- Sorted Workers by Sales (Highest to Lowest) ---"); Console.WriteLine("Name\t\tAge\tDistrict\tSales\tMonthly Pay"); Console.WriteLine("-----------------------------------------------------"); foreach (Worker worker in sortedWorkers) { // Calculate level for display string level; if (worker.Sales > 199) level = "Level 4"; else if (worker.Sales >= 100) level = "Level 3"; else if (worker.Sales >= 50) level = "Level 2"; else level = "Level 1"; Console.WriteLine($"{worker.Name}\t\t{worker.Age}\t{worker.District}\t\t{worker.Sales}\t{worker.MonthlyPay}kr ({level})"); }
Key Improvements Explained
- Worker Class: Groups all related data together, making it easy to manage and sort.
- List Storage: Lets us keep track of multiple workers instead of just one.
- Loop: The
forloop runs exactly 4 times, so you don't have to repeat the code 4 times manually. - Error Handling: Used
int.TryParseto prevent crashes if the user enters non-numeric values (a good habit to learn early!). - Sorting: LINQ makes sorting a list super straightforward with
OrderByDescending.
Don't hesitate to ask if any part doesn't make sense—learning takes time, and you're doing great!
内容的提问来源于stack exchange,提问作者jamelime

