Value is not added to a variable within a function

Asked

Viewed 46 times

1

I’m doing a simple quiz show with questions and answers, with punctuation. My problem is this: I made a function so that every time the answer is right, +1 will be added to the score variable.

public static int pontu1 { get; set; }

    public static int pont(int pontu1)
    {
        pontu1++;
        return pontu1;
    }

So apparently, it’s working, I’ve decided to create the "right answer" function, so every time the answer is right, this function will be called:

public static void rcerta()
    {
        Console.WriteLine("Resposta certa");
        pont(pontu1);
        System.Threading.Thread.Sleep(2000);
        Console.Clear();
    }

Problem is, I went to test the code, sent the question, put the right answer. Okay, the function ran normal, gave right answer, however, when I went to see how much was my score, with the following code (it sits inside a switch from a menu I made) :

case 2:
                    Console.WriteLine("Sua pontuação é: " + pontu1);
                    Console.WriteLine("Pressione ENTER para continuar....");
                    break;

The score appears as 0, even with the right answer. Can anyone help me? Follow the whole program and the class below.

Program:

class Program
{
    public static int pontu1 { get; set; }

    public static int pont(int pontu1)
    {
        pontu1++;
        return pontu1;
    }

    public static void rcerta()
    {
        Console.WriteLine("Resposta certa");
        pont(pontu1);
        System.Threading.Thread.Sleep(2000);
        Console.Clear();
    }

    public static void rerrada()
    {
        Console.WriteLine("Resposta errada");
        Console.Clear();
    }

    public static void resetar()
    {
        string r;
        Console.WriteLine("Sua pontuação atual é: " + pontu1);
        Console.WriteLine("Deseja reiniciar sua pontuação?      S  / N");
        r = Console.ReadLine();
        if (r == "S")
        {
            pontu1 = 0;
            Console.WriteLine("Pontuação reiniciada.");
            Console.WriteLine("Sua pontuação atual é: " + pontu1);
            Console.WriteLine("Pressione ENTER para voltar ao menu...");
        }
        else
        {
            Console.WriteLine("Pontuação mantida.");
            Console.WriteLine("Sua pontuação atual é: " + pontu1);
            Console.WriteLine("Pressione ENTER para voltar ao menu...");
        }
    }

    public static void QuizIncio()
    {
        int opcao;
        Console.WriteLine("Escolha um assunto!");
        Console.WriteLine("[ 1 ] teste");
        Console.WriteLine("[ 2 ] teste2");
        Console.WriteLine("-------------------------------------");
        Console.Write("Digite uma opção: ");
        opcao = Int32.Parse(Console.ReadLine());
        switch (opcao)
        {
            case 1:
                Class1.teste();
                break;
            case 2:
                break;
            default:
                break;
        }
        Console.ReadKey();
        Console.Clear();
    }
    static void Main(string[] args)
    {
        int opcao;
        do
        {
            Console.WriteLine("[ 1 ] Iniciar");
            Console.WriteLine("[ 2 ] Ver Pontuação");
            Console.WriteLine("[ 3 ] Resetar Pontuação");
            Console.WriteLine("[ 0 ] Sair do Programa");
            Console.WriteLine("-------------------------------------");
            Console.Write("Digite uma opção: ");
            opcao = Int32.Parse(Console.ReadLine());
            switch (opcao)
            {
                case 1:
                    Console.Clear();
                    QuizIncio();
                    break;
                case 2:
                    Console.WriteLine("Sua pontuação é: " + pontu1);
                    Console.WriteLine("Pressione ENTER para continuar....");
                    break;
                case 3:
                    Console.Clear();
                    resetar();
                    break;
                case 0:
                    break;
            }
            Console.ReadKey();
            Console.Clear();
        }
        while (opcao != 0); 
    }




}

Class:

class Class1 : Program
{
    public static void teste()
    {
        string r1, r2, r3, r4, r5;
        Console.WriteLine("Quanto é 2 + 2?");
        Console.WriteLine("a) 4");
        Console.WriteLine("b) 3");
        r1 = Console.ReadLine();
        if (r1 == "a")
        {
            rcerta();
        }
        else
        {
            rerrada();
        }
    }
}

1 answer

1


This code is very confusing and full of problems. I won’t even try to fix it because it would be too much work and the focus of the question is just one. If the code were more organized the error would not occur. You are using the same name for the class property and for the local parameter of the method that has priority, so you are adding to the local variable that disappears at the end of the method execution, and you do not touch the property. I changed the name of the property, which is even more according to the style of C# and the problem was solved.

using static System.Console;
using System.Threading.Tasks;

public class Program {
    public static int Pontu1 { get; set; }

    public static int IncrementaPontos(int pontu1) {
        Pontu1++;
        return pontu1;
    }

    public static void RespostaCerta() {
        WriteLine("Resposta certa");
        IncrementaPontos(Pontu1);
        Task.Delay(2000);
//        Console.Clear();
    }

    public static void RespostaErrada() {
        WriteLine("Resposta errada");
        Clear();
    }

    public static void Resetar() {
        WriteLine("Sua pontuação atual é: " + Pontu1);
        WriteLine("Deseja reiniciar sua pontuação?      S  / N");
        string r = ReadLine();
        if (r == "S")  {
            Pontu1 = 0;
            WriteLine("Pontuação reiniciada.");
        } else WriteLine("Pontuação mantida.");
        WriteLine("Sua pontuação atual é: " + Pontu1);
        WriteLine("Pressione ENTER para voltar ao menu...");
    }

    public static void QuizIncio() {
        WriteLine("Escolha um assunto!");
        WriteLine("[ 1 ] teste");
        WriteLine("[ 2 ] teste2");
        WriteLine("-------------------------------------");
        Write("Digite uma opção: ");
        if (!int.TryParse(ReadLine(), out int opcao)) return;
        switch (opcao) {
            case 1:
                Class1.Teste();
                break;
            case 2:
                break;
        }
 //       Console.Clear();
    }

    public static void Main(string[] args) {
        int opcao;
        do
        {
            WriteLine("[ 1 ] Iniciar");
            WriteLine("[ 2 ] Ver Pontuação");
            WriteLine("[ 3 ] Resetar Pontuação");
            WriteLine("[ 0 ] Sair do Programa");
            WriteLine("-------------------------------------");
            Write("Digite uma opção: ");
            if (!int.TryParse(ReadLine(), out opcao)) continue;
            switch (opcao) {
                case 1:
 //                   Console.Clear();
                    QuizIncio();
                    break;
                case 2:
                    WriteLine("Sua pontuação é: " + Pontu1);
                    WriteLine("Pressione ENTER para continuar....");
                    break;
                case 3:
//                    Clear();
                    Resetar();
                    break;
                case 0:
                    break;
            }
//            Console.Clear();
        } while (opcao != 0); 
    }
}

class Class1 : Program {
    public static void Teste() {
        WriteLine("Quanto é 2 + 2?");
        WriteLine("a) 4");
        WriteLine("b) 3");
        string r1 = ReadLine();
        if (r1 == "a") RespostaCerta();
        else RespostaErrada();
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

I got a good upgrade, but I can still do more.

Browser other questions tagged

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