Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Could use some help with my code

Hey everyone, I am following the newer C# course and am at the end of the Conditions video.

I came up with this to write the program to have the player solve math problems, but when I run it nothing shows up in the console.

Answers

  • VinnyCodesVinnyCodes Member
    edited February 5

    Here is the code I made that (hopefully) fixed your problem:

    int problem1 = 2 * 5 + 3;

        int problem2 = 4 * 4 + 2;

        int problem3 = 7 * 6 + 5;

       

            Console.WriteLine("Please solve the following problems:");

            Console.Write("2 * 5 + 3 = ");


            int answer1 = Convert.ToInt32(Console.ReadLine());


            if (answer1 == problem1){

              Console.WriteLine("Correct!");

            }

            else{

                Console.WriteLine("Incorrect!");

            }


            Console.Write("4 * 4 + 2 = ");


            int answer2 = Convert.ToInt32(Console.ReadLine());


            if (answer2 == problem2){

                Console.WriteLine("Correct!");

            }

            else{

                Console.WriteLine("Incorrect!");

            }


            Console.Write("7 * 6 + 5: = ");


            int answer3 = Convert.ToInt32(Console.ReadLine());


            if (answer3 == problem3){

                Console.WriteLine("Correct!");

            }

            else{

                Console.WriteLine("Incorrect!");

            }



    Console.ReadKey();


    As far as I understand, which is not very much, the 'answer' variable was reading the first line. So you could type a number in the empty console and that would be your answer to all the problems. I moved the answer variable to below the Console.WriteLine() and change to to answer1, then below problem 2, there is answer2, etc... Also, the putting Console.WriteLine(problem1) just gives the user the answer to the equation instead of equation itself. So I changed the Console.WriteLine to the equation and not the answer to the equation. Hopefully that is everything.

Sign In or Register to comment.