将多个用户输入添加到List c#

时间:2015-02-18 23:37:15

标签: c# list input

我试图从用户那里获得用户输入,直到用户没有输入任何内容(所以按下回车键)但它似乎没有正常工作。用户应该能够添加他们想要的数量,并且一旦他们点击输入键而没有输入数字就应该显示它们。

代码:

using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.Collections.Generic;

namespace Lab2
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> numbersInput = new List<string>();

            Console.WriteLine("Please enter an integer");
            string input = Console.ReadLine();
            numbersInput.Add(input);


            while (input != "")
            {
                Console.WriteLine("Please enter another integer: ");
               input = Console.ReadLine();
            }

            if (input == "")
            {
                Console.WriteLine("The number you have entered is: " + " " + input);
                numbersInput.Add(input);
                foreach (string value in numbersInput)
                {
                    Console.WriteLine("The number that was added to the list is : " + " " + value);
                }
                Console.ReadLine();
            }
        }
    }
}

5 个答案:

答案 0 :(得分:3)

除了空字符串外,您没有向numbersInput列表中添加任何内容。

 if (input == "") // Why do anything with input if you enter this block?
 {
     Console.WriteLine("The number you have entered is: " + " " + input);
     numbersInput.Add(input);
     foreach (string value in numbersInput)

numbersInput.Add(input)需要在while块中。

试试这个

while (input != "")
{
    Console.WriteLine("Please enter another integer: ");
    input = Console.ReadLine();
    numbersInput.Add(input);
}

if (input == "")
{
    foreach (string value in numbersInput)
    {
        Console.WriteLine("The number that was added to the list is : " + " " + value);
    }
    Console.ReadLine();
}

编辑:用于求和

更改您的列表声明。

List<int> numbersInput = new List<int>();

然后解析数字并将它们添加到列表中。如果解析失败,则需要处理错误。

while (input != "")
{
    Console.WriteLine("Please enter another integer: ");
    input = Console.ReadLine();
    int value;
    if(!int.TryParse(input, out value))
    {
       // Error
    }
    else
    {
       numbersInput.Add(value);
    }
}

然后你的列表不再是字符串,所以改变foreach

int sum = 0;
foreach (int value in numbersInput)
{
    sum += value;
    Console.WriteLine("The number that was added to the list is : " + " " + value.ToString());
}

答案 1 :(得分:0)

尝试将input != ""替换为!String.IsNullOrEmpty(input)input==""StringIsNullOrEmpty(input)

喜欢这个:

&#13;
&#13;
 List<string> numbersInput = new List<string>();

        Console.WriteLine("Please enter an integer");
        string input = Console.ReadLine();

        while (!String.IsNullOrEmpty(input))
        {

            Console.WriteLine("Please enter another integer: ");
           input = Console.ReadLine();

        }


        if (String.IsNullOrEmpty(input))
        {
            Console.WriteLine("The number you have entered is: " + " " + input);
            numbersInput.Add(input);
            foreach (string value in numbersInput)
            {
                Console.WriteLine("The number that was added to the list is : " + " " + value);
            }
            Console.ReadLine();
        }
&#13;
&#13;
&#13;

答案 2 :(得分:0)

  1. 不要将字符串与&#34;&#34;进行比较,而是使用string.IsNullOrEmpty()或string.IsNullOrWhitespace()(假设您的目标是.NET Framework 2.0或更高版本。)

  2. 您有不必要的代码(final if语句),它不提供任何值。

  3. 除此之外,您的代码需要重新构建。

    这可能是您正在寻找的:

    using System;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Collections;
    using System.Collections.Generic;
    
    namespace Lab2
    {
        class Program
        {
            static void Main(string[] args)
            {
                List<string> numbersInput = new List<string>();
    
                Console.WriteLine("Please enter an integer: ");
                string input = Console.ReadLine();
    
                while (!string.IsNullOrEmpty(input))
                {
                    numbersInput.Add(input);
                    Console.WriteLine("Please enter another integer: ");
                    input = Console.ReadLine();
                }
    
                if (numbersInput.Count > 0)
                {
                    Console.WriteLine("You have entered " + numbersInput.Count + " numbers, they were: ");  
                    foreach (var input in numbersInput)
                    {
                        Console.WriteLine("\t" + input);
                    }
                }
                else
                {
                    Console.WriteLine("You have entered 0 numbers.");  
                }
    
            }
        }
    }
    

    HTH

答案 3 :(得分:0)

您没有将numberInput添加到列表中:

        static void Main(string[] args)
    {

        String input;
        Int32 n_In, i = 1;
        List<Int32> user_Inputs = new List<int>();

        while ((input = Console.ReadLine()).Length > 0)
            if (int.TryParse(input, out n_In)) user_Inputs.Add(n_In);

        Console.WriteLine("Numbers entered: ");
        if (user_Inputs.Count == 0) return;

        foreach (Int32 n in user_Inputs)
            Console.WriteLine("Number" + i++ + ": " + n);

        Console.ReadKey();
    }

答案 4 :(得分:0)

非常短的版本,不会添加您的个人逻辑,但应该展示这个想法:

using System;
using System.Collections.Generic;

namespace Test
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            List<string> listofstrings = new List<string> ();
            string input = null;

            while ((input = Console.ReadLine ()) != string.Empty) {
                listofstrings.Add (input);
            }
        }
    }
}