C# Method and variable call

Asked

Viewed 63 times

-1

I am making a wish in c#, I made the methods of questions but when it comes to checking the correct alternatives is not recording the score in the variable points, what I am doing wrong?

using System;
using System.Windows.Forms;

namespace Quiz
{
    public partial class QuizComidas : Form
    {
        private int pontos = 0;

        public QuizComidas()
        {
            InitializeComponent();
     
        }
        
        private void Pergunta2()
        {
            LblQuestao.Text = "Questão 2";
            lblPergunta.Text = "Em que Pais foi inventada a feijoada?";
            lblResposta.Text = "";

            btbAlt1.Text = "Italia";
            btbAlt2.Text = "Brasil";
            btbAlt3.Text = "Argentina";
            btbAlt4.Text = "Portugal";

            btbAlt1.Checked = false;
            btbAlt2.Checked = false;
            btbAlt3.Checked = false;
            btbAlt4.Checked = false;

            if (btbAlt4.Checked == true)
            {

                pontos += 1;
                pontos++;
            }

        } 

        private void Pergunta3()
        {
            LblQuestao.Text = "Questão 3";
            lblPergunta.Text = "O acarajé foi inventado aonde?";
            lblResposta.Text = "";

            btbAlt1.Text = "Bahia";
            btbAlt2.Text = "Maranhão";
            btbAlt3.Text = "São Paulo";
            btbAlt4.Text = "Salvador";

            btbAlt1.Checked = false;
            btbAlt2.Checked = false;
            btbAlt3.Checked = false;
            btbAlt4.Checked = false;

            if (btbAlt2.Checked == true)
            {

                pontos = pontos + 1;
            }
        }

        private void Pergunta4()
        {
            LblQuestao.Text = "Questão 4";
            lblPergunta.Text = "A Caipirinha tem "+"\n"+
                                "origem de qual estado brasileiro?";
        

            btbAlt1.Text = "Mato Grosso";
            btbAlt2.Text = "Acre";
            btbAlt3.Text = "São Paulo";
            btbAlt4.Text = "Rio Grande Do Sul";

            btbAlt1.Checked = false;
            btbAlt2.Checked = false;
            btbAlt3.Checked = false;
            btbAlt4.Checked = false;

            if (btbAlt3.Checked == true)
            {

                pontos = pontos + 1;
            }
        }

        private void Pergunta5()
        {
            LblQuestao.Text = "Questão 5";
            lblPergunta.Text = "Você vai na região Sul do país e come"+"\n"+
                                "uma fruta Bergamota."+"\n"+
                                "Uma Bergamota é o mesmo que:";

            btbAvançar.Text = "Finalizar";
            btbAlt1.Text = "Laranja";
            btbAlt2.Text = "Maça";
            btbAlt3.Text = "Mexerica";
            btbAlt4.Text = "Uva";

            btbAlt1.Checked = false;
            btbAlt2.Checked = false;
            btbAlt3.Checked = false;
            btbAlt4.Checked = false;

            if (btbAlt3.Checked == true)
            {

                pontos = pontos + 1;
            }
        }


        private void lblPergunta_Click(object sender, EventArgs e)
        {

        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void btbCorreto_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void QuizComidas_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            
        }

        private void btbAvançar_Click(object sender, EventArgs e)
        {

            if(LblQuestao.Text == "Questão 1")
            {
                Pergunta2();
                

            } 
           
              else  if (LblQuestao.Text == "Questão 2") 
            {
                Pergunta3();
       
            }
                else if(LblQuestao.Text == "Questão 3")
            {
                Pergunta4();
            }
                else if(LblQuestao.Text == "Questão 4")
            {
                Pergunta5();
              
            }
            else if(btbAvançar.Text == "Finalizar")
            {
                MessageBox.Show("Sua Pontuação: "+ pontos, "Pontuação",
             MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            }
            }
            
        }
   

  • Vitoria, as I understand it, when you click on the forward button you are calling the method of the next question, but it has not yet been answered, so it does not record the answers. You have tested whether lblQuestao.Text is question 1 executed the method of question two, but you have answered question 1 and not question 2. In your if of question 1, call the method of question 1 and proceed to question 2, in your question if 2 call the question 2 method and proceed to question 3 and so on...

  • I’ll try, thank you

1 answer

0


The first problem is that you are checking the answer at the same time you are creating the question. As was said in the Adjair comments.

You should not check the answer in the methods Question1(), Question2(), etc...

These methods should be responsible only for creating the questions.

The second problem is that once you are sharing the answer buttons among all the questions, the correct answer will depend on the current question.

So create a variable called private int respostaCorreta; and in each question creation method, store which button has the correct answer to this question. For example in the method Pergunta2() attribute respostaCorreta = 4; in the method Pergunta3() attribute respostaCorreta = 2; and so on

Once this is solved, implement the events by clicking the four response buttons and make sure that the button clicked is the corresponding one stored in the Return variable. Positive case increase points pontos++;

Ex:

 private void radioButton1_CheckedChanged(object sender, EventArgs e)
 {
    if (respostaCorreta == 1)
    {
        pontos++;
    }
 }

 private void radioButton2_CheckedChanged(object sender, EventArgs e)
 {
    if (respostaCorreta == 2)
    {
        pontos++;
    }
 }

and so on.

PS: Check the name of the methods because the controls have a default name and the other methods (btbAlt1 and radioButton1_CheckedChanged) the method should be called btbAlt1_CheckedChanged

  • Thank you so much, now you’re telling and you’re all right

Browser other questions tagged

You are not signed in. Login or sign up in order to post.