c#切换问题。家庭作业

时间:2013-10-20 16:34:29

标签: c#

这是我到目前为止所拥有的。我的问题是,当您输入正确或错误的答案时,没有一个案例正在响应。我不确定从哪里开始。程序会要求您回答两个随机数相乘。然后它应该给你八个回复中的一个。

        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;

3 个答案:

答案 0 :(得分:2)

caseSwitch始终为0,因此无需向控制台写入任何内容,您的开关将始终通过。

如果您想要随机响应,您可以执行以下操作:

    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;

答案 1 :(得分:1)

CaseSwitch始终= 0。 您需要为其指定一个值,并为交换机添加默认大小写。

答案 2 :(得分:0)

您有int caseSwitch = 0;,我没有看到您在代码中将其更改为1-4中的任何一个。那么如果你没有更改caseSwitch,你期望它做什么......