Adding multiple user input to a List c#

2019-04-16 18:58发布

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

标签: c# list input
5条回答
Evening l夕情丶
2楼-- · 2019-04-16 19:40
  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

查看更多
对你真心纯属浪费
3楼-- · 2019-04-16 19:44

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());
}
查看更多
虎瘦雄心在
4楼-- · 2019-04-16 19:51

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();
    }
查看更多
何必那么认真
5楼-- · 2019-04-16 19:53

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);
            }
        }
    }
}
查看更多
手持菜刀,她持情操
6楼-- · 2019-04-16 19:56

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

查看更多
登录 后发表回答