C# 开关问题.作业

本文关键字:作业 问题 开关 | 更新日期: 2023-09-27 18:36:04

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

        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;

C# 开关问题.作业

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;

CaseSwitch 始终 = 0。您需要为其分配一个值,和/或向交换机添加默认大小写。

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