简体   繁体   中英

How do I compare the elements in the array with the elements entered in the textbox sequentially in c#?

My goal is to actually make a guessing game, so I created two arrays with Mysql data called answers and questions. And what I want to do is take the value from the user and if it is true, for example my first answer 'fashion' matches the guess the user entered in the textbox, I want the label to write correct and continue with the next answer and try to find the next answer


My code returns true when I enter my values in the array into the textbox, but I want them to be in order. How do you think I can use the for loop. How do you think I can use the for loop to make an ordered comparison?

          for (int i=0;i<cevaplar.Count;i++)
        {
            string tahmin = textBox1.Text;
            if(cevaplar.Contains(tahmin))
            {
                label1.Text = "true";
                continue;
                
                
            }
            else
            {
                label1.Text = "false";
                break;

            }
        }
  

    }

In your code you use "cevaplar.Contains(tahmin)" . With contains you're checking if tahim can be found anywhere in your array, without taking any order in account.

The solution to your problem should be quite simple. Just don't use contains in this situation but use a simple indexer to compare the elements. Try the following:

Replace:

if(cevaplar.Contains(tahmin))
            {
                ...
            }

With

if(cevaplar[i] == tahim) //here you check only if the i'th element is matching.
            {
                ...
            }

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM