You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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 _highest2 is inconsistent. For example, you set _highest = _fishWeight when it's larger than _highest2, then immediately overwrite _highest again 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 _highest1 and _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 highest1 and highest2 to decimal.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.
  • 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_

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:34:25