1

I have just started learning C# with one of brackey's tutorials, and I have tried to make a simple quiz. I have tried everything I could have thought to do (basically deleting instances of the variables I was having trouble with), and I can't find anything online that I can understand or implement into my current scenario.

using System;

namespace Learning_C_
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Are you ready to solve some math equations?");
         
            // I'm not sure what this indent is
            if (Console.ReadLine() == "Yes")
            {
                Console.WriteLine("Definitely Generating Math Equations");
            } else {
                Console.WriteLine("Well thats too bad... You have too anyways");
            }

            Console.Write("Solve for x\n5x + 5 = 25\nx = ");
            int ans01 = Convert.ToInt32(Console.ReadLine());
            bool correct01 = false;

            switch (ans01) {
                case 4:
                    bool correct01 = true;
                    Console.WriteLine("Wow, you got it correct!\nThat's 1/3");
                    break;
                default:
                    bool correct01 = false;
                    Console.WriteLine("Oops! You got that one incorrect...\n0/3");
                    break;
            }

            Console.Write("Solve\n75 + 2 * 12.5 = ");
            int ans02 = Convert.ToInt32(Console.ReadLine());
            bool correct02 = false;

            switch (ans01){
                case 100:
                    bool correct02 = true;
                    if(correct01 == true){
                        Console.WriteLine("Wow, you got it correct!\nThat's 2/3");
                    } else {
                        Console.WriteLine("Wow, you got it correct!\nThat's 1/3");
                    }
                    break;
                default:
                    bool correct02 = false;
                    if (correct01 == true)
                    {
                        Console.WriteLine("Oops! You got that one incorrect...\n1/3");
                    }
                    else 
                    {
                        Console.WriteLine("Oops! You got that one incorrect...\n0/3");
                    }
                    break;
            }

            Console.Write("Simplify\n5(4n + 3) = 23\nAnswer: ");
            string ans03 = Convert.ToString(Console.ReadLine());

            switch (ans03){
                case "n = 1":
                    if(correct01 == true || correct02 == true){
                        Console.WriteLine("Wow, you got it correct!\nThat's 2/3");
                    } else if (correct01 == true && correct02 == true){
                        Console.WriteLine("Wow, you got it correct!\nThat's 3/3, congrats! You aced this test!");
                    }
                    break;
                default:
                    if (correct01 == true || correct02 == true)
                    {
                        Console.WriteLine("Oops! You got that one incorrect...\n1/3");
                    }
                    else if(correct01 == true && correct02 == true){
                        Console.WriteLine("Oops! You got that one incorrect...\n2/3");
                    }
                    break;
            }

            // Wait before closing
            Console.ReadKey();
        }
    }
}

My issue is I can't seem to reference these correct01 and 02 variables so that I can display the correct amount of points. I'm not sure if this is because they are defined in the switch statement but I can't seem to use them anywhere else. VS Code is also complaining that they are not used in the switch statement themselves.

Overall, I need a way to reference the correct01 and 02 variables from inside the switch statements.

Woody1193
  • 7,252
  • 5
  • 40
  • 90
  • 2
    You can't, which is essentially the *point* of encapsulation. You'll want to *define* the variables outside the `switch`, then update the *values* inside of it. Just remove `bool` from the inner assignments. – Obsidian Age Jun 15 '22 at 23:08

1 Answers1

2

Because you had decalred two variable with the same name.

Remove the bool that is in the switch statement

  bool correct01 = false;

                    switch (ans01){
                        case 4:
                            correct01 = true;
                            Console.WriteLine("Wow, you got it correct!\nThat's 1/3");
                            break;
                        default:
                            correct01 = false;
                            Console.WriteLine("Oops! You got that one incorrect...\n0/3");
                            break;
                    }
HsuTingHuan
  • 615
  • 1
  • 7
  • 23