[英]Adding multiple user input to a List c#
我正在尝试从用户那里获取用户输入,直到用户什么都不输入(因此请按Enter键),但它似乎无法正常工作。 用户应该能够添加任意数量的数字,并且在他们按Enter键且没有输入数字后,它应该显示它们。
码:
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
列表中。
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());
}
尝试更换input != ""
有!String.IsNullOrEmpty(input)
和input==""
与StringIsNullOrEmpty(input)
。
像这样:
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(); }
不要将字符串与“”进行比较,而应使用string.IsNullOrEmpty()或string.IsNullOrWhitespace()(假设您的目标是.NET Framework 2.0或更高版本。)
您有没有提供任何值的不必要的代码(if语句末尾)。
除此之外,您的代码需要重组。
这可能是您要寻找的:
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.");
}
}
}
}
高温超导
您没有将numberInputs添加到列表中:
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();
}
非常简短的版本,不会添加您的个人逻辑,但应演示以下想法:
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);
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.