c# - input numbers, first one deciding how many there will be -


I have to insert some numbers from the console input into an array, where the first number decides how many (the number is * 2). The number has a maximum of 2 digits. My code is still not working anyway till now.

  int volume = Convert.ToInt32 (console. Read ()); int [] arr = new int [100]; Int count = 0; While (counting> lie; volume * 2) {string line = Console.ReadLine (); Line = row + ''; For (int i = 0; i  = '0') & amp; amp; (c & lt; = '9')) {char d = row [i + 1]; If ((D> = '0') & amp; amp; (D & lt; = '9')) {arr [count] = c * 10 + d; I ++; } And {arr [count] = c; } Calculate ++; }}    

There are some errors that allow your program to work correctly Can not give. Do not use

  // console. Read, but wait to see the end of input from your user string user input console. Readline (); // Convert any number by using Int32.TryParse to actually write a number / / nothing to your user / and there will be no such exception that if this is not a number; If (int32.TryParse (user input, volume out)! {Console.WriteLine ("An integer number is required!"); Return; } // Dimension To keep the size of the array larger for size / the required numbers in the following loop (I'm not sure that you will now have to double the size of the array) int [] arr = New int [volume * 2] ; Int count = 0; While (counting> lie; volume * 2) {string line = Console.ReadLine (); Int input number; // To convert any number again, use Int32. Trippers if (Int32.TryParse (line, input number)) {arr [count] = inputNumber; Count ++; } else {Console.WriteLine ("An integer number is required!"); }}   

can better handle user input, there is no need to work with individual characters searching for invalid input. (By the way, your actual method does not enlarge your code by two digits)

However, in such a context, when you do not know the exact size of the array, the recommended data structure is used To list & lt; Int & gt;

  // Make a list of integers, the size is not required, the list is   

You can easily work with a list because it is an array for

  (int x = 0; x & lt; arr.Qount; x ++) console.reete line (ARE [x]. ostring ());   

Edit after your comment below. If there are many numbers in the line then you need to split on user input, but this does not affect TryParse probes, you just need an additional loop and require a control over the maximum number of included elements

  int [] arr = new int [quantity]; Int count = 0; While (counting & quot; volume) {string line = Console.ReadLine (); Int input number; // Split on space and tab, and as a result // remove empty elements if there are two consecutive places / tabs string [] Parts = line. Split (new four [] {'', '\ t'}, string split option. RemoveEmptyEntries); Foreach (a number number in parts) {If (Int32.TryParse (line, input number)) {arr [count] = inputNumber; Count ++; } Other {console. WrightLine (aNumber.ToString () + "is not an integer number!"); } If (calculation> gt; arr.Length) breaks; }}    

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -