+ 2

How to take multiple inputs into a list until a certain word is typed?

I'm trying to do a script that will take an integer and add it to a list. It will continue to ask for an integer and add it until you type 'exit'. At that time it will sort the list and give you the lowest and highest integer. I feel like a while loop will do this for me, but I'm unsure on the proper way to do it. The code below is what I have, but it simply stops after I enter the first integer. The main part that is screwing me up is trying to detect when someone types 'exit' and catch any exceptions. List<int> nums = new List<int>(); Console.WriteLine("Enter a number or exit: "); var input = Console.ReadLine().ToLower(); while (input != "exit") { nums.Add(Convert.ToInt32(input)); } nums.Sort(); Console.WriteLine("Your biggest number is -{0}- and your smallest number is -{1}-.", nums[nums.Count - 1], nums[0]);

7th Oct 2017, 2:58 AM
Soygeezy
Soygeezy - avatar
3 ответов
+ 9
Use Do...While Loop and Take your inputs inside the loop and check the condition at the end of the do...while loop
7th Oct 2017, 3:31 AM
Abhishek Pun
Abhishek Pun - avatar
+ 3
Be aware of the fact. That if you this in the playground you need to give all inputs before the start of the run.
7th Oct 2017, 9:50 AM
sneeze
sneeze - avatar