如何通过阅读用户输入将变量添加到我的列表?

时间:2015-05-24 00:30:58

标签: c# list console

我是C#的初学者,想通过我的列表添加。阅读用户输入法。

class Program
{
    static void Main(string[] args)
    {
        List<int> list = new List<int>();


        Console.WriteLine("Asking a random Question here");

        // lacking Knowledge here **
         ** = Convert.ToInt16(Console.ReadLine());

        Console.ReadKey();
    }

3 个答案:

答案 0 :(得分:2)

我想你想要

int

System.Int32是一个32位数字,对应app.use(express.static('public')); after app.get('*', function(req, res){ res.send('Bad Route'); }); ;

答案 1 :(得分:1)

No * evaluate(No * head)
{
    No *tmp = head;   // no new !  It's a pointer, just take the value of the head
    ...  // as before
    while (tmp != NULL)
    {
        if (tmp->ID == projectID){
             int evaluation;
             cout<<"Please insert the evaluation\n";
             cin >> evaluation;
             tmp->evaluation = evaluation;
             return head;  // We return the full list (head) 
        }
        tmp = tmp->next;
    }
    cout <<"Project not found\n";
    return head;
}

Console.ReadLine()方法确实返回一个字符串值,在您的情况下,您希望将用户输入的值添加到int列表中。

所以基本上你必须:

class Program
{
static void Main(string[] args)
{
    List<int> list = new List<int>();


    Console.WriteLine("Asking a random Question here");

    // lacking Knowledge here **
     ** = Convert.ToInt16(Console.ReadLine());

    Console.ReadKey();
}

然后将数字添加到列表中,如下所示:

Int32 number = Convert.ToInt32(Console.ReadLine());

答案 2 :(得分:1)

因此,为了问题的详细示例,这里的程序将平均标准输入中列出的数字,每行一个。它还显示了如何将给定数字添加到列表中。

    using System;
    using System.Collections.Generic;

    namespace AverageNumbers
    {
        class MainClass
        {
            public static void Main (string[] args)
            {
                // Here is the list of numbers
                List<int> numbers = new List<int>();

                // Here are two variables to keep track
                // of the input number (n), and the sum total (sum)
                int n, sum = 0;
                // This while loop waits for user input and converts
                // that input to an integer
                while (int.TryParse(Console.ReadLine(), out n))
                {
                    // Here we add the entered number to the sum
                    sum += n;
                    // And to the list to track how many we've added       
                    numbers.Add(n);
                }

                // Finally we make sure that we have more than 0 numbers that we're summing and write out their average
                Console.WriteLine("Average: " + (numbers.Count > 0? sum / numbers.Count : 0));
            }
        }
    }