C#控制台:如何输出输入N个数的前两大值?代码调试求助
Fixing Your C# Console App to Find the Top Two Largest Values
Hey there! Let's break down why your current code isn't working and fix it so you can correctly get the top two largest weights from your input.
What's Wrong with Your Original Code?
Let's go through the issues step by step:
- No loop for multiple inputs: Your code only reads one weight, not N numbers. You can't find the top two values if you only process a single entry!
- Broken logic for updating top values: The way you're assigning
_highest,_highest1, and_highest2is inconsistent. For example, you set_highest = _fishWeightwhen it's larger than_highest2, then immediately overwrite_highestagain if it's smaller than_highest1—this doesn't properly track the two largest values. - Missing variable initialization: It looks like you didn't initialize
_highest1and_highest2(they default to 0). If all your input weights are negative, this will give wrong results.
Corrected Code with Explanations
Here's a revised version that handles multiple inputs and correctly tracks the top two largest values:
// Initialize top two values to the smallest possible decimal (works for negative weights too) decimal highest1 = decimal.MinValue; decimal highest2 = decimal.MinValue; Console.WriteLine("How many fish weights do you want to enter?"); if (int.TryParse(Console.ReadLine(), out int inputCount) && inputCount > 0) { for (int i = 0; i < inputCount; i++) { Console.WriteLine($"Enter weight #{i + 1}:"); if (decimal.TryParse(Console.ReadLine(), out decimal fishWeight)) { // Update top two values if current weight is larger than existing ones if (fishWeight > highest1) { // Push the old highest value down to second place highest2 = highest1; // Current weight becomes the new highest highest1 = fishWeight; } // Check if current weight is larger than the second highest (but not equal to first, optional) else if (fishWeight > highest2 && fishWeight != highest1) { highest2 = fishWeight; } // Uncomment below if you want to allow duplicate top values (e.g., two identical max weights) // else if (fishWeight == highest1) // { // highest2 = fishWeight; // } } else { Console.WriteLine("Oops, that's not a valid number. Please try again."); i--; // Re-try the current input slot } } // Display the results Console.WriteLine("\nHere are the top two highest weights:"); Console.WriteLine($"1st Highest: {highest1}"); Console.WriteLine($"2nd Highest: {highest2}"); } else { Console.WriteLine("Please enter a valid positive number for the count of weights."); }
Key Logic Explained
- Initialization: By setting
highest1andhighest2todecimal.MinValue, we ensure even negative weights are handled correctly. - Input Loop: We first ask how many weights you want to enter, then loop that many times to collect each value.
- Updating Top Values:
- When a new weight is larger than the current highest (
highest1), we move the old highest to second place (highest2) and set the new weight as the new highest. - If the new weight is larger than the second highest but not equal to the first, we update
highest2.
- When a new weight is larger than the current highest (
- Input Validation: If the user enters an invalid number, we prompt them to try again without skipping an input slot.
Testing the Code
Try entering values like 5.2, 3.7, 6.1, 4.9—the code should output 6.1 as the highest and 5.2 as the second highest. If you enter duplicates like 7.0, 7.0, 5.5, it will show 7.0 for both (if you uncomment the duplicate handling logic).
内容的提问来源于stack exchange,提问作者Nikssj_
相关产品推荐
相关产品推荐

