作业管理程序开发:如何输出匹配对应科目的已排序截止日期?
Hey there! Let's break down what's going wrong with your current code and how to fix it so your homework entries stay paired with their correct deadlines after sorting.
The Problem with Your Current Approach
Right now, you're storing subject, homework content, and deadlines in three separate lists: subject, homework, and deadline. When you call deadline.Sort(), you only reorder the deadline values—but the subject and homework lists stay in their original order. That means when you loop through all three lists later, the entries no longer match up (e.g., the first subject might now be paired with the third deadline).
The Solution: Group Related Data Together
Instead of keeping three separate lists, you should create a class to bundle all the details for a single homework entry. This way, when you sort, the entire homework record (subject + content + deadline) stays intact.
Step 1: Define a Homework Class
First, create a simple class to hold all the related information:
Public Class Homework ' Properties to store each part of the homework entry Public Property Subject As String Public Property Content As String Public Property DaysUntilDue As Integer End Class
Step 2: Rewrite the Input Logic
Instead of adding items to three separate lists, create a new Homework object for each entry and add it to a single list:
Console.WriteLine("Welcome to my homework organiser. How many pieces of homework do you have?") ' Parse the input to an integer (add error handling if needed!) Dim repeats As Integer = Integer.Parse(Console.ReadLine()) ' Create a single list to hold all homework entries Dim homeworkEntries As New List(Of Homework)() For i = 0 To repeats - 1 Dim newHomework As New Homework() Console.WriteLine("Enter subject " & i + 1) newHomework.Subject = Console.ReadLine() Console.WriteLine("Enter homework " & i + 1) newHomework.Content = Console.ReadLine() Console.WriteLine("How many days until " & newHomework.Subject & "'s homework is due?") newHomework.DaysUntilDue = Integer.Parse(Console.ReadLine()) ' Add the complete entry to our list homeworkEntries.Add(newHomework) Next
Step 3: Sort the Entries by Deadline
Now you can sort the entire list of Homework objects by their DaysUntilDue property. We'll use a lambda function to define the sorting logic:
' Sort the list so entries with the shortest deadline come first homeworkEntries.Sort(Function(entry1, entry2) entry1.DaysUntilDue.CompareTo(entry2.DaysUntilDue))
Step 4: Output the Sorted Entries
Finally, loop through the sorted list and print each complete entry—everything will stay paired correctly!
For Each entry In homeworkEntries Console.WriteLine($"{entry.Subject}, {entry.Content}, {entry.DaysUntilDue} days") Next
Optional: Add Error Handling
To make your program more robust, you should add checks for invalid input (e.g., if the user enters text instead of a number for the number of homework pieces or days until due). Here's a quick example of how to handle that for the repeat count:
Dim repeats As Integer Do Console.WriteLine("Welcome to my homework organiser. How many pieces of homework do you have?") Loop Until Integer.TryParse(Console.ReadLine(), repeats) AndAlso repeats > 0
This ensures the user enters a valid positive integer before proceeding.
内容的提问来源于stack exchange,提问作者Fully Random

