There is a series of 15 questions that are submitted to the screen according to the following conditions:

if (i % 2 == 0){} if (i % 3 == 0){} if (i % 5 == 0){} 

How to exclude an overlay of questions, namely to exclude the fulfillment of conditions with common divisors, for example, the numbers 6,10,12,15.

Code to modify else if:

 if (i % 3 == 0 && i != 0) { if (i % 2 == 0) { } else if (j < dataGridView2.Rows.Count - 1) { } } 

Fragment of the issue code:

 if(i==0 || i==1 || i % 2 == 0) { if (f < dataGridView1.Rows.Count - 1) { // QuestionTime = DateTime.Now; textBox1.Visible = false; radioButton1.Visible = true; radioButton2.Visible = true; radioButton3.Visible = true; groupBox1.Visible = false; this.Text = Transfer; label1.Text = dataGridView1.Rows[f].Cells[2].Value.ToString(); QuestionVariantString = dataGridView1.Rows[f].Cells[3].Value.ToString(); QuestionSplit = QuestionVariantString.Split(';'); QuestionTrueChoice = dataGridView1.Rows[f].Cells[4].Value.ToString(); radioButton1.Text = QuestionSplit[0]; radioButton2.Text = QuestionSplit[1]; radioButton3.Text = QuestionSplit[2]; button1.Enabled = false; f++; } } else if (i % 3 == 0 && i != 0) { if (j < dataGridView2.Rows.Count - 1) { // QuestionTime = DateTime.Now; textBox1.Text = ""; radioButton1.Visible = false; radioButton2.Visible = false; radioButton3.Visible = false; groupBox1.Visible = false; textBox1.Visible = true; label1.Text = dataGridView2.Rows[j].Cells[2].Value.ToString(); QuestionTrueChoice = dataGridView2.Rows[j].Cells[3].Value.ToString(); QuestionSense = textBox1.Text; j++; } } else if (i % 5 == 0 && i != 0) { if (g < dataGridView3.Rows.Count - 1) { // QuestionTime = DateTime.Now; radioButton1.Visible = false; radioButton2.Visible = false; radioButton3.Visible = false; groupBox1.Visible = true; label1.Text = dataGridView3.Rows[g].Cells[2].Value.ToString(); QuestionVariantString = dataGridView3.Rows[g].Cells[3].Value.ToString(); QuestionSplit = QuestionVariantString.Split(';'); QuestionTrueChoice = dataGridView3.Rows[g].Cells[4].Value.ToString(); checkBox1.Text = QuestionSplit[0]; checkBox2.Text = QuestionSplit[1]; checkBox3.Text = QuestionSplit[2]; checkBox4.Text = QuestionSplit[3]; g++; } } 

When I = 6, the conditions, as I understand it, are fulfilled twice and, as a result, the question is duplicated.

  • one
    Using else if seems to solve the problem. Or I misunderstand the question ... - Vladimir Martyanov
  • Modified but still does not work as it should. - SAIBERPRO
  • Divide into prime numbers? - tym32167
  • Yes, the rotation is from 0 to 15. Like and painted on paper and the numbers are written out common but can not be deleted. It turns out that from 9 active positions 6 position is traversed 2 times. Hence zazhevyvaya 9 position. - SAIBERPRO
  • @SAIBERPRO show the code with the modification under else if , most likely it was not so recorded - yolosora

1 answer 1

Not sure what you have to do. But he put on a basic example.

 for (var i = 1; i <= 15; i++) { if (i % 2 == 0 && i % 3 == 0 && i % 5 == 0) { Console.WriteLine($"{i} div on 2, 3, 5"); } else if (i % 2 == 0 && i % 3 == 0) { Console.WriteLine($"{i} div on 2, 3"); } else if (i % 2 == 0 && i % 5 == 0) { Console.WriteLine($"{i} div on 2, 5"); } else if (i % 3 == 0 && i % 5 == 0) { Console.WriteLine($"{i} div on 3, 5"); } else if (i % 2 == 0) { Console.WriteLine($"{i} div on 2"); } else if (i % 3 == 0) { Console.WriteLine($"{i} div on 3"); } else if (i % 5 == 0) { Console.WriteLine($"{i} div on 5"); } else { Console.WriteLine($"{i} no any div"); } } 
  • I wanted to add +, but I can’t log your theoretically almost close to what I need and in my opinion even it, I will try to rewrite the code in the same way and check your logic. Thank! - SAIBERPRO 1:16 pm