将多个用户输入添加到列表C# [英] Adding multiple user input to a List c#

查看:59
本文介绍了将多个用户输入添加到列表C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从用户那里获得用户输入,直到用户没有输入任何内容(因此请按Enter键),但它似乎无法正常工作.用户应该可以根据需要添加任意数量的数字,并且一旦按下Enter键且没有输入数字,它将显示它们.

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.

代码:

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

推荐答案

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

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)必须位于while块中.

numbersInput.Add(input) needs to be in the while block instead.

尝试一下

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

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

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

然后您的列表不再是字符串,因此更改foreach

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

这篇关于将多个用户输入添加到列表C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆