-1

所以我有一个整数数组。我想使用循环并排除使等式成立的整数。所以这就像

for (int n = 0; n < first9char.Length; n++ ) {
                                        if (first9char[n] == clickValue) {
                                            first9char[n] = first9char[n + 1];

但是它只会改变等于不改变整个数组的值。那么有没有办法做到这一点?

我想在这个循环中使用它。

            if (UserSquareMethod.clickedBoxes[0] == -1) {
            MessageBox.Show("You have not selected any box");
        } else {
            int i = 0;
            do {
                if (textButtonArray[clickedBox[i]].Text == "" || clickValue == "") {
                    textButtonArray[clickedBox[i]].Text = clickValue;
                    textButtonArray[clickedBox[i]].Font = new Font(textButtonArray[clickedBox[i]].Font.FontFamily, 14, FontStyle.Bold);
                }

                else
                {
                    textButtonArray[clickedBox[i]].Text += "," + clickValue;
                    textButtonArray[clickedBox[i]].Font = new Font(textButtonArray[clickedBox[i]].Font.FontFamily, 5, FontStyle.Regular);
                    string[] first9char = textButtonArray[clickedBox[i]].Text.Split(new string[] { "," }, StringSplitOptions.None);
                    for (int j = 1; j < first9char.Length; j++)
                    {
                        for (int k = j - 1; k >= 0; k--)
                        {
                            if (first9char[j] == first9char[k])
                            {
                                if (clearNumberClicked == true)
                                {
                                    first9char = Array.FindAll(first9char, x => x != clickValue);
                                    label2.Text = first9char[0];

                                    //int n = 0;

                                    //for (int p = 0; p < first9char.Length; p++)
                                    //{
                                      //  if (first9char[p] != clickValue)
                                      //  {
                                        //    first9char[n] = first9char[p];
                                        //    n++;
                                        //    label2.Text += "," + first9char[n];
                                       // }
                                   // }



                                    //for (int n = 0; n < first9char.Length; n++ ) {
                                        //if (first9char[n] == clickValue) {
                                          //  first9char[n] = first9char[n + 1];
                                          //  for ( int p = 0; p < n; p++) {

                                                //}

                                        //}
                                    //}
                                    MessageBox.Show("Clear the number" + first9char[(first9char.Length - 1)] + "and " + clickValue + " " + first9char.Length);

                                }

                                else {
                                first9char[j] = "";
                                textButtonArray[clickedBox[i]].Text = first9char[0]; 
                                MessageBox.Show("You cannot enter the same number again!"+ first9char[j]+j);
                                for (int m = 1; m < (first9char.Length - 1); m++) {
                                    textButtonArray[clickedBox[i]].Text += ","+ first9char[m]; 

                                }
                            }


                            }
                        }

                    }



                    if (textButtonArray[clickedBox[i]].Text.Length > 9)
                    {

                        textButtonArray[clickedBox[i]].Text = first9char[0] + "," + first9char[1] + "," + first9char[2] + "," + first9char[3] + "," + first9char[4];
                        MessageBox.Show("You cannot enter more than 5 numbers, please clear the box if you want to enter different number." + textButtonArray[clickedBox[i]].Text.Length);
                    }

                }
                i++;

            }
            while (clickedBox[i] != -1);


        }

    }
4

1 回答 1

3

我会为此使用 LINQ:

first9char = first9char.Where(x => x != clickValue).ToArray();

它只是意味着“选择不匹配的项目”。如果由于某种原因您不能使用 LINQ,那么只需保留另一个计数器,并确保仅从n那里循环到:

int n = 0;

for(int i = 0; i < first9char.Length; i++) {
    if(first9char[i] != clickValue) {
        first9char[n] = first9char[i];
        n++;
    }
}

清洁高效。

于 2012-05-28T23:25:34.617 回答