none
C# point system is not working. RRS feed

  • Question

  • static void Main(string[] args)
            {

                Ja(0);
            }

            static void Ja(int point)
            {
                Random numbergenerator = new Random();
                Random numbergenerator2 = new Random();
                int num01 = numbergenerator.Next(1, 11);
                int num02 = numbergenerator.Next(1, 11);
                int antwoord = num01 + num02 + numbergenerator2.Next(-1, 1);

                Console.WriteLine(num01 + "+" + num02 + "=" + antwoord);
                int answer = Convert.ToInt32(Console.ReadLine());

                // the point system is not working...


                {
                    if (num02 + num01 == antwoord)
                    {
                        if (answer == 1)
                        {

                            Ja(point++);
                        }
                        else
                        {
                            Console.WriteLine("Game Over " + point);
                        }

                    }

                    else
                    {
                        if (answer == 0)
                        {

                            Ja(point++);
                        }
                        else
                        {
                            Console.WriteLine("Game Over " + point);
                        }
                    }

                }

                Console.ReadLine();

            }

    I know the reason that it doesn't work. becus it is a loop and the int "point" always return to 0 but how to fix this? How to make a point system in this case that it don't become 0 at the end.

                                                                                       
    Tuesday, April 2, 2019 6:34 PM

Answers

All replies

  • static void Main(string[] args)
            {

                Ja(0);
            }

            static void Ja(int point)
            {
                Random numbergenerator = new Random();
                Random numbergenerator2 = new Random();
                int num01 = numbergenerator.Next(1, 11);
                int num02 = numbergenerator.Next(1, 11);
                int antwoord = num01 + num02 + numbergenerator2.Next(-1, 1);

                Console.WriteLine(num01 + "+" + num02 + "=" + antwoord);
                int answer = Convert.ToInt32(Console.ReadLine());

                // the point system is not working...


                {
                    if (num02 + num01 == antwoord)
                    {
                        if (answer == 1)
                        {

                            Ja(point++);
                        }
                        else
                        {
                            Console.WriteLine("Game Over " + point);
                        }

                    }

                    else
                    {
                        if (answer == 0)
                        {

                            Ja(point++);
                        }
                        else
                        {
                            Console.WriteLine("Game Over " + point);
                        }
                    }

                }

                Console.ReadLine();

            }

    I know the reason that it doesn't work. becus it is a loop and the int "point" always return to 0 but how to fix this? How to make a point system in this case that it don't become 0 at the end.

                                                                                       

    I'm not 100% sure of what you are trying to accomplish, but if you try this 
    amended version of your code you should start to see some points.

    Note that because you had the Console.ReadLine() inside the method which is
    called recursively you would need to keep pressing Enter at the end based on
    the number of times the Ja method was called. So I moved it to Main.

    class Program
    {
        static int zero = 0;
        static void Main(string[] args)
        {
            //Ja(0);            
            Ja(ref zero);
            Console.ReadLine();
        }
    
        //static void Ja(int point)
        static void Ja(ref int point)
        {
            Random numbergenerator = new Random();
            Random numbergenerator2 = new Random();
            int num01 = numbergenerator.Next(1, 11);
            int num02 = numbergenerator.Next(1, 11);
            int antwoord = num01 + num02 + numbergenerator2.Next(-1, 1);
    
            Console.WriteLine(num01 + "+" + num02 + "=" + antwoord);
            int answer = Convert.ToInt32(Console.ReadLine());
    
            // the point system is not working...
    
            {
                if (num02 + num01 == antwoord)
                {
                    if (answer == 1)
                    {
    
                        //Ja(point++);
                        point++;
                        Ja(ref point);
                    }
                    else
                    {
                        Console.WriteLine("Game Over " + point);
                    }
    
                }
    
                else
                {
                    if (answer == 0)
                    {
    
                        //Ja(point++);
                        point++;
                        Ja(ref point);
                    }
                    else
                    {
                        Console.WriteLine("Game Over " + point);
                    }
                }
    
            }
    
            //Console.ReadLine();
    
        }
    }
    

    - Wayne

    Tuesday, April 2, 2019 7:52 PM
  • Try ‘Ja(point + 1)’ or ‘Ja(++point)’ instead of ‘Ja(point++)’.

    Wednesday, April 3, 2019 5:07 AM
  • thank you :)

    Thursday, April 4, 2019 7:43 PM