Adding multiple user input to a List c#

2019-04-16 18:55发布

问题:

I'm trying to get user input from the user until the user enters nothing (so a enter key press) but it doesn't seem to be working properly. The user should be able to add as many numbers as they'd like and it should display them once they hit the enter key with no number entered.

Code:

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();
            }
        }
    }
}

回答1:

You're not adding anything to the numbersInput list except empty strings.

 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) needs to be in the while block instead.

Try this

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();
}

Edit: For summing

Change your List declaration.

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

Then parse the numbers out and add them to the list. If the parsing fails, you need to handle the error.

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

Then your list is no longer a string, so change the foreach

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


回答2:

You are NOT adding the numberInputs to the list:

        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();
    }


回答3:

  1. Do not compare strings to "", instead use string.IsNullOrEmpty() or string.IsNullOrWhitespace() (assuming you are targeting .NET Framework 2.0 or later.)

  2. You have unnecessary code (final if statement) which does not provide any value.

Aside from that, your code needs to be restructured.

This is probably what you're looking for:

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



回答4:

try replace input != "" with !String.IsNullOrEmpty(input) and input=="" with StringIsNullOrEmpty(input).

Like This:

 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();
        }



回答5:

Very short version, does not add your personal logic but should demonstrate the idea:

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);
            }
        }
    }
}


标签: c# list input