将多个用户输入添加到List c#
我试图从用户那里获得用户输入,直到用户没有输入任何内容(所以按下回车键),但它似乎没有正常工作。 用户应该能够添加任意数量的数字,并且一旦他们点击输入键而没有输入数字就应该显示它们。
码:
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 numbersInput = new List(); 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 numbersInput = new List ();
然后解析数字并将它们添加到列表中。 如果解析失败,则需要处理错误。
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()); }
您没有将numberInput添加到列表中:
static void Main(string[] args) { String input; Int32 n_In, i = 1; List user_Inputs = new List(); 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(); }
-
不要将字符串与“”进行比较,而是使用string.IsNullOrEmpty()或string.IsNullOrWhitespace()(假设您的目标是.NET Framework 2.0或更高版本。)
-
您有不必要的代码(final 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 numbersInput = new List (); 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
尝试使用StringIsNullOrEmpty(input)
替换input != ""
!String.IsNullOrEmpty(input)
和input==""
。
像这样:
List numbersInput = new List (); 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(); }
非常短的版本,不添加你的个人逻辑,但应该certificate这个想法:
using System; using System.Collections.Generic; namespace Test { class MainClass { public static void Main (string[] args) { List listofstrings = new List (); string input = null; while ((input = Console.ReadLine ()) != string.Empty) { listofstrings.Add (input); } } } }