Began to master recently C #. It is necessary to make a program that would guess the number that the user made. The user can click the "more" or "less" buttons. There is an array of random numbers from which a number is randomly selected. I can not understand a little how to program the "less" button so that when it is pressed, numbers from the array are output only those that are smaller than the number that was displayed to the user.

public Form1() { InitializeComponent(); int[] array = new int[20]; Random rnd = new Random(); for (int i = 1; i < array.Length; i++) { array[i] = rnd.Next(0, 20); label1.Text = array[new Random().Next(0, array.Length)].ToString(); } } 

Here is the random number picker from the array.

    2 answers 2

    You need to make the array variable a field of the Form1 class, and also make the hidden number a field:

     public partial class Form1 : Form { private int[] array = new int[20]; private Random pickRnd = new Random(); private int pickedNumber; public Form1() { InitializeComponent(); Random rnd = new Random(); for (int i = 1; i < array.Length; i++) { array[i] = rnd.Next(0, 20); } pickedNumber = PickNumber(); label1.Text = pickedNumber.ToString(); } private int PickNumber() { return array[pickRnd.Next(0, array.Length)]; } } 

    Then you can access them from other methods (in particular, from the handlers for pressing buttons):

     private void btnLess_Click(object sender, EventArgs e) { // оставляем только числа меньше загаданного array = array.Where(i => i < pickedNumber).ToArray(); // загадываем новое число pickedNumber = PickNumber(); label1.Text = pickedNumber.ToString(); } 

    With the "More" button is similar, only you need to select larger numbers.

    • That is, an array and a variable with a random number must be rendered before the form? - Andrei
    • @Andrey outside the form designer . Updated the answer. - andreycha
    • And all. I understood. Just did not look. Thank you very much. - Andrei
    • @ Andrei is not at all. - andreycha
    • And how to correctly withdraw it? Instead of the number displays system.int, 32 [] - Andrew

    There is an array of random numbers from which a number is randomly selected.

    I would not advise using this algorithm, since it is extremely effective. I recommend using the binary search algorithm, and always check the limits of the valid range (0.20) https://ru.wikipedia.org/wiki/%D0%94%D0%B2%D0%BE%D0%B8%D1% D0% BD% D1% 8B% D0% B9_% D0% BF% D0% BE% D0% B8% D1% 81% D0% BA

    • There's also a simple appeal on the index. - andreycha
    • And if you are talking about “finding all the numbers less than the planned one”, then firstly the array must be sorted for binary search, and secondly the 20 elements are the same. What kind of inefficiency are we talking about? - andreycha
    • Well, first of all, a person learns, and the method proposed by him is not the best for solving this problem. I suggested a more optimal algorithm. And secondly, when using a binary search, he will not need an array at all, only the boundaries of the range will be needed. - whoismaikl