The program should work like this:

Asks the user for the length of the array, the user enters. Asks what type of array the user needs (int or string), the user chooses. Next, an array is created with the desired data type, and a length that it indicated above. And then the program simply writes Finish!

What is the incomprehensible error of the program? For some reason, after creating the array, it again asks for its length ... And only then writes Finish! Why it happens? It seems everything wrote correctly ...

Here is the code:

using System; namespace Launcher { class Program { public static void Main(string[] args) { dlin_ARRAY(); tip_ARRAY(); Console.WriteLine("Finish!"); Console.Read(); } public static int dlin_ARRAY() { Console.WriteLine("Введи длину массива"); Console.WriteLine(">>>"); int userinput_dlin_ARRAY = int.Parse(Console.ReadLine()); Console.WriteLine("<<<"); return userinput_dlin_ARRAY; } public static void tip_ARRAY() { Console.WriteLine("Введи цифру типа массива, который тебе нужен"); Console.WriteLine("1) Integer"); Console.WriteLine("2) Stringer"); bool isTrue = true; while (isTrue) { Console.WriteLine(">>>"); int userinput_tip_ARRAY = int.Parse(Console.ReadLine()); Console.WriteLine("<<<"); if (userinput_tip_ARRAY == 1) { int[] array = new int[dlin_ARRAY()]; } else if (userinput_tip_ARRAY == 2) { string[] array = new string[dlin_ARRAY()]; } else { Console.WriteLine("Не верно, введи 1 или 2"); continue; } isTrue = false; } } } } 
  • You do not write the result of the execution of the dlin_ARRAY() method into the following method, in which you call the method again: new int/string[dlin_ARRAY()] . So there are no bugs. The program does exactly what you wrote - Kir_Antipov
  • Count how many times you call the dlin_ARRAY() method, and understand why it asks length many times - tym32167
  • Well, it is clear that I somehow call this method several times, but I don’t understand how to make it run only once in Main () ... - Tikhon Fedulov
  • @TikhonFedulov I wrote to you. Cache the result of its execution in Main and pass as a parameter to your second method, where substitute the transferred value instead of the method calls - Kir_Antipov

1 answer 1

As correctly noted in the comments, you do not use the return value of the dlin_Array() method. And then you call this method again, and if you enter 3 for the first time when defining an array type, your dlin_Array method will be called at least 3 times.

I would call it in the tip_Array method and assign it to a variable:

 using System; namespace Launcher { class Program { public static void Main(string[] args) { tip_ARRAY(); Console.WriteLine("Finish!"); Console.Read(); } public static void tip_ARRAY() { var lengthArray = dlin_ARRAY(); Console.WriteLine("Введи цифру типа массива, который тебе нужен"); Console.WriteLine("1) Integer"); Console.WriteLine("2) Stringer"); while (true) { Console.WriteLine(">>>"); int userinput_tip_ARRAY = int.Parse(Console.ReadLine()); Console.WriteLine("<<<"); if (userinput_tip_ARRAY == 1) { int[] array = new int[lengthArray]; break; } else if (userinput_tip_ARRAY == 2) { string[] array = new string[lengthArray]; break; } else { Console.WriteLine("Не верно, введи 1 или 2"); } } } public static int dlin_ARRAY() { Console.WriteLine("Введи длину массива"); Console.WriteLine(">>>"); int userinput_dlin_ARRAY = int.Parse(Console.ReadLine()); Console.WriteLine("<<<"); return userinput_dlin_ARRAY; } } } 
  • Not critical of course, but why call him there? Why not pass there the result of its implementation? - tym32167 pm
  • @ tym32167 According to Martin, "Function Arguments" In the ideal case, the number of function arguments is zero (the null-ary function). Next, the functions with one argument (unary) and two arguments (binary). The functions with three arguments (ternary) follow possibility to avoid. The need for functions with a large number of arguments (polyaric) must be supported by very high arguments - and still it is better not to use such functions. " - user218976 pm
  • Well, Martin, his recommendations, undoubtedly, are mostly useful, but he is not always right (especially his recommendations on adding a state instead of adding a method parameter), because doing some things just because Uncle Bob said it is not worth it. In addition, he wrote that a function should do only 1 functional thing, and you, in fact, added functions to call a different function without any need, which in theory violates SRP (and the function no longer corresponds to its name). But, as I said, this is not critical, I was interested in your opinion, thank you :) - tym32167
  • @ tym32167 I didn’t modify the author’s code very much, because it would have confused him (the author). And I did not enter an extra argument into the function, which in turn again complicates the understanding of the function (albeit slightly). - user218976
  • @ tym32167 Why do we need to enter an extra argument into a function (allocate memory for it), if we can choose an application architecture in which we can do without introducing this argument? Exactly for the same reason, I removed the isTrue boolean variable from the author code. - user218976 2:12 pm