C#需要使用switch语句添加随机响应

时间:2015-01-31 00:40:48

标签: c# random switch-statement case

namespace _7._39
{
    class Program
    {
        static void Main(string[] args)
       {
        string response1;
        string response2;
        string response3;
        string response4;

        Random resp = new Random();

        bool correct = Question();// Create a value to call the question method

        if (correct== true)// when the answer is true it return very good
        {
            Console.WriteLine("Very Good!");
        }
        else// when the answer is false it returns to try again
        {
            Console.WriteLine("No please try again");
        }
    }
    public static bool Question()
    {
        Random rand = new Random();// we create a random number
        int num = rand.Next(1, 9);// first random number between 1 and 9
        int num1 = rand.Next(1, 9);// second random number between 1 and 9

        int ans = num * num1;// the value of multiplication between 1 and 2

        // asking what the two values are multiplied
        Console.WriteLine("What is"+ num.ToString()+ "*" +num1.ToString());
        // reads the users attempt 
        int attempt = int.Parse(Console.ReadLine());


        if (attempt == ans)// when the attempt is equal to the answer
        {
            return true;// its returns true bool
        }

        else// if it is false it says no please try again
        {
            Console.WriteLine("No please try again");
            return Question();// and creates a new question for the user
        }
    }
}
}

我需要正确的== true和false来回应4种可能的选择中的随机响应。我需要通过执行switch语句来发出每个响应。同样通过使用随机选择出现的响应。

非常好!

出色!

干得好!

保持良好的工作!

和4个错误响应选项

如何将此代码实现到我当前的代码中?

response = resp.Next(1, 5);
                switch (response)
                {
                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;
                default;

            }

1 个答案:

答案 0 :(得分:1)

试试这个:

var rnd = new Random();
Func<bool, string> getRespose = b =>
{
    var choices = b
        ? new [] { "Very good!", "Excellent!", "Nice work!", "Keep up the good work!", }
        : new [] { "Bad!", "V Bad!", "VV Bad!", "VVV Bad!", };

    return choices[rnd.Next(0, choices.Length)];
};

无需switch声明。


或者如果您想将其作为switch

var rnd = new Random();
var choices = (string[])null;
switch (correct)
{
    case true:
        choices = new []
            { "Very good!", "Excellent!", "Nice work!", "Keep up the good work!", };
        break;
    case false:
        choices = new [] { "Bad!", "V Bad!", "VV Bad!", "VVV Bad!", };
        break;
}

var response = choices[rnd.Next(0, choices.Length)];

或者,使用switchFunc

var rnd = new Random();
Func<bool, string> getRespose = b =>
{
    var choices = (string[])null;
    switch (b)
    {
        case true:
            choices = new []
                { "Very good!", "Excellent!", "Nice work!", "Keep up the good work!", };
            break;
        case false:
            choices = new []
                { "Bad!", "V Bad!", "VV Bad!", "VVV Bad!", };
            break;
    }

    return choices[rnd.Next(0, choices.Length)];
};

var response = getRespose(correct);

或作为&#34;正常&#34;功能:

private Random rnd = new Random();
private string GetRespose(bool b)
{
    var choices = (string[])null;
    switch (b)
    {
        case true:
            choices = new []
                { "Very good!", "Excellent!", "Nice work!", "Keep up the good work!", };
            break;
        case false:
            choices = new []
                { "Bad!", "V Bad!", "VV Bad!", "VVV Bad!", };
            break;
    }

    return choices[rnd.Next(0, choices.Length)];
}

或作为&#34;正常&#34;功能,但没有switch

private Random rnd = new Random();
private string GetRespose(bool b)
{
    var choices = b
        ? new [] { "Very good!", "Excellent!", "Nice work!", "Keep up the good work!", }
        : new [] { "Bad!", "V Bad!", "VV Bad!", "VVV Bad!", };

    return choices[rnd.Next(0, choices.Length)];
}