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

基于UserOption枚举创建double类型列表的程序构建技术问询

Let's Build Your Double List Tool Step by Step

Hey there! I get it—method structure and calling them from Main can feel confusing at first, but once we walk through this example, it'll click. Let's build exactly what you need: a program with a double list that lets users add values (with real-time printing), calculate the sum, and pick actions from a menu.

First, the Full Working Code (C# Example)

I'll show you the complete code first, then we'll unpack each part so you understand how it all connects:

using System;
using System.Collections.Generic;
using System.Linq;

class DoubleListManager
{
    static void Main(string[] args)
    {
        // Initialize our double list in Main—this is where our data lives
        List<double> numberList = new List<double>();
        bool keepRunning = true;

        // Main menu loop to let users choose actions
        while (keepRunning)
        {
            Console.WriteLine("\nChoose an option:");
            Console.WriteLine("1. Add a value to the list");
            Console.WriteLine("2. Calculate and print the sum");
            Console.WriteLine("3. Exit");
            Console.Write("Enter your choice (1/2/3): ");
            
            string choice = Console.ReadLine();

            switch (choice)
            {
                case "1":
                    // Call our method to add a value—pass the list to it
                    AddValueToList(numberList);
                    break;
                case "2":
                    // Call our sum method, get the result, then print it
                    double total = CalculateListSum(numberList);
                    Console.WriteLine($"Total sum of the list: {total:F2}");
                    break;
                case "3":
                    keepRunning = false;
                    Console.WriteLine("Goodbye!");
                    break;
                default:
                    Console.WriteLine("Invalid choice—please enter 1, 2, or 3.");
                    break;
            }
        }
    }

    // Method to handle adding a value to the list (with real-time printing)
    static void AddValueToList(List<double> list)
    {
        Console.Write("Enter a double value to add: ");
        string input = Console.ReadLine();
        
        // Validate the input so the program doesn't crash
        if (double.TryParse(input, out double value))
        {
            list.Add(value);
            Console.WriteLine($"Successfully added *{value:F2}* to the list!");
        }
        else
        {
            Console.WriteLine("Oops, that's not a valid number. Try again.");
        }
    }

    // Method to calculate the sum of the list and return it
    static double CalculateListSum(List<double> list)
    {
        // Option 1: Use LINQ's Sum() method (simple and clean)
        return list.Sum();

        // Option 2: Manual loop (great for understanding how summing works under the hood)
        // double sum = 0;
        // foreach (double num in list)
        // {
        //     sum += num;
        // }
        // return sum;
    }
}

Breaking Down the Key Concepts

Let's go through the parts that matter most for your understanding:

  • Main Function as the "Control Center": This is where the program starts. We initialize our List<double> here because this is the core data we'll work with. The while loop keeps the program running until the user chooses to exit.
  • Calling Methods from Main: When the user picks "Add a value", we call AddValueToList(numberList)—we pass our list to the method because List is a reference type, so changes made in the method (like adding a value) affect the original list in Main.
  • Method Responsibilities: Each method does one specific job:
    • AddValueToList: Handles input validation, adds the value to the list, and prints the real-time confirmation.
    • CalculateListSum: Takes the list, computes the sum, and returns the result to Main (where we then print it). This keeps the method focused on calculation, not printing.
  • Real-Time Printing: When a valid value is added, we immediately print it using Console.WriteLine right in the AddValueToList method—this gives the user instant feedback.

How This Solves Your Problem

  • You get a double list that stores user values.
  • Users can choose to add values (with real-time print feedback) or calculate the sum.
  • The code clearly shows how methods interact with Main: passing data to methods, getting results back, and keeping logic organized.

If you want to tweak this (like adding a "print all values" option) or have questions about any part, feel free to ask!

内容的提问来源于stack exchange,提问作者dpa76

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 03:38:43