Zac Petrikenas
Zac Petrikenas

Reputation: 11

c# Switch issue. Homework

This is what I have so far. My problem is that none of the cases are responding when you enter either the correct or incorrect answer. I'm not really sure where to go from here. The program asks you answer two random numbers being multiplied. And then it should give you one of the eight responses.

        int result = 0;
        int caseSwitch = 0;

        string question = DoMultiplication(out result);
        Console.WriteLine(question);
        int answer = Convert.ToInt32(Console.ReadLine());

        if (answer == result)
        {
            switch (caseSwitch)
            {
                case 1:
                    Console.WriteLine("Very Good");
                    break;

                case 2:
                    Console.WriteLine("Excellent");
                    break;

                case 3:
                    Console.WriteLine("Nice Work");
                    break;

                case 4:
                    Console.WriteLine("Keep up the good work!");
                    break;

            }

        }
        else
        {
            switch (caseSwitch)
            {


                case 1:
                    Console.WriteLine("No, Please Try Again.");
                    break;

                case 2:
                    Console.WriteLine("Wrong, Try Once More");
                    break;

                case 3:
                    Console.WriteLine("Don't Give Up!");
                    break;

                case 4:
                    Console.WriteLine("No, Keep Trying!");
                    break;

Upvotes: 0

Views: 105

Answers (3)

geedubb
geedubb

Reputation: 4057

caseSwitch is always 0, so your switch will always fall through without writing anything to console.

If you want a random response you could do something like this:

    int result = 0;
    int caseSwitch = new Random().Next(1, 4);

    string question = DoMultiplication(out result);
    Console.WriteLine(question);
    int answer = Convert.ToInt32(Console.ReadLine());

    if (answer == result)
    {
        switch (caseSwitch)
        {
            case 1:
                Console.WriteLine("Very Good");
                break;

            case 2:
                Console.WriteLine("Excellent");
                break;

            case 3:
                Console.WriteLine("Nice Work");
                break;

            case 4:
                Console.WriteLine("Keep up the good work!");
                break;

        }

    }
    else
    {
        switch (caseSwitch)
        {


            case 1:
                Console.WriteLine("No, Please Try Again.");
                break;

            case 2:
                Console.WriteLine("Wrong, Try Once More");
                break;

            case 3:
                Console.WriteLine("Don't Give Up!");
                break;

            case 4:
                Console.WriteLine("No, Keep Trying!");
                break;

Upvotes: 2

Ms. Nobody
Ms. Nobody

Reputation: 1219

You have your int caseSwitch = 0; and I don't see you changing it in your code to any of 1-4. So what do you expect it to do if you dont have the caseSwitch changed...

Upvotes: 0

Justin Lessard
Justin Lessard

Reputation: 11911

CaseSwitch is always = 0. You need to assign a value to it, and-or add a default case to your switch.

Upvotes: 1

Related Questions