#c# #console-application #console.writeline
Вопрос:
Мы пытаемся написать 3 массива на консоли
подобный этому;
DDDEEDUDE
ЭДУДЕУ
DUUEUEDD
случайный выбор массивов и букв заданного кода выполняется полностью нормально, но отображается только первая строка. Мне как-то не пришла в голову блестящая идея. Первый массив работает идеально, но другие массивы не отображаются.
string[] A1 = new string[15]; string[] A2 = new string[15]; string[] A3 = new string[15]; Random rastgele = new Random(); int selected_array = 0; int gamecount_for_a1 = 0; int gamecount_for_a2=0; int gamecount_for_a3=0; string which_letter_str; while (true) { int which_array = rastgele.Next(3); if (which_array == 0) { selected_array = 0;//A1 } else if (which_array == 1) { selected_array = 1;//A2 } else { selected_array = 2;//A3 } int which_letter_int= rastgele.Next(3); if (which_letter_int == 0) { which_letter_str = "D"; } else if (which_letter_int == 1) { which_letter_str = "E"; } else { which_letter_str = "U"; } if (selected_array ==0) { A1[gamecount_for_a1] = which_letter_str; Console.SetCursorPosition(gamecount_for_a1, 0); Console.Write(A1[gamecount_for_a1]); gamecount_for_a1 ; } else if (selected_array == 1) { A1[gamecount_for_a2] = which_letter_str; Console.SetCursorPosition(gamecount_for_a2, 2); Console.Write(A2[gamecount_for_a2]); gamecount_for_a2 ; } else { A1[gamecount_for_a3] = which_letter_str; Console.SetCursorPosition(gamecount_for_a3, 4); Console.Write(A3[gamecount_for_a3]); gamecount_for_a3 ; } Console.ReadLine(); if (gamecount_for_a1 == 15 || gamecount_for_a2 == 15 || gamecount_for_a3 == 15) break; }
Ответ №1:
В вашем коде есть две ошибки:
A1[gamecount_for_a2] A1[gamecount_for_a3]
Массивы должны быть A2 и A3.
Ответ №2:
Вы можете изменить свой код следующим образом:
const int ArrayLength = 15; const int A1_Count_Index = 0; const int A2_Count_Index = 1; const int A3_Count_Index = 2; const int SpaceBetweenArrayDisplays = 2; string[] A1 = new string[ArrayLength]; string[] A2 = new string[ArrayLength]; string[] A3 = new string[ArrayLength]; string[][] ArraysInPlay = new string[][] { A1, A2, A3 }; string[] Letters = new[] { "D", "E", "U" }; int[] GameCounts = new int[] { 0, 0, 0 }; //index 1--gt; count for A1, index 2 --gt; count for A2, index 3 count for A3 Random rastgele = new Random(); while (GameCounts[A1_Count_Index] lt; ArrayLength amp;amp; GameCounts[A2_Count_Index] lt; ArrayLength amp;amp; GameCounts[A3_Count_Index] lt; ArrayLength) { var selected_array_index = rastgele.Next(ArraysInPlay.Length); var letter_index = rastgele.Next(Letters.Length); var gameCount = GameCounts[selected_array_index]; var cursorPosition = selected_array_index * SpaceBetweenArrayDisplays; // if selected array 0--gt; 0, if 1--gt; 2, if 2 --gt; 4 var selectedArray = ArraysInPlay[selected_array_index]; selectedArray[gameCount] = Letters[letter_index]; Console.SetCursorPosition(gameCount, cursorPosition); Console.Write(selectedArray[gameCount]); gameCount ; GameCounts[selected_array_index] = gameCount; } // move cursor to the bottom line Console.SetCursorPosition(0, ArraysInPlay.Length * SpaceBetweenArrayDisplays); Console.ReadLine();