0

我们正在尝试在控制台上编写 3 个数组

像这样;

DDDEEDUDE

教育

到期

随机选择给定的数组和字母代码运行得很好,但它只显示第一行。我想不出一个好主意。第一个数组运行完美,但其他数组不显示。

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;
}




4

2 回答 2

1

您的代码中有两个错误:

A1[gamecount_for_a2]
A1[gamecount_for_a3]

数组应该是 A2 和 A3。

于 2021-11-16T18:00:36.753 回答
0

您可以将代码重构为以下内容:

    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--> count for A1, index 2 --> count for A2, index 3 count for A3

    Random rastgele = new Random();

    while (GameCounts[A1_Count_Index] < ArrayLength && GameCounts[A2_Count_Index] < ArrayLength && GameCounts[A3_Count_Index] < 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--> 0, if 1--> 2, if 2 --> 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();
于 2021-11-16T19:10:52.527 回答