C#需要使用switch语句添加随机响应
我需要我的correct==true和false在4个可能的选择中随机响应。我需要通过执行switch语句来发出每个响应。也可以使用随机选择出现的响应 非常好 好极了 干得好 继续努力 以及4个错误响应选项 如何将此代码实现到当前代码中C#需要使用switch语句添加随机响应,c#,random,switch-statement,case,C#,Random,Switch Statement,Case,我需要我的correct==true和false在4个可能的选择中随机响应。我需要通过执行switch语句来发出每个响应。也可以使用随机选择出现的响应 非常好 好极了 干得好 继续努力 以及4个错误响应选项 如何将此代码实现到当前代码中 namespace _7._39 { class Program { static void Main(string[] args) { string response1; strin
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
}
}
}
}
试试这个:
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;
}
或者,使用开关和函数
:
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)];
或作为“正常”功能,但不带开关
:
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)];
};
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)];
}
你还没真正问过问题。有关更多信息,请参阅。您的问题是什么?使用new Random().Next()
是有风险的。您应该创建一个new Random()
实例,将其传递给您的处理程序(如Question
)并在共享实例上调用。Next()
。我想使用开关,因为我知道这是一种方法,我看到您的解决方案可以工作,不过,我还不了解C#中的函数。@Bonetoad-我为您添加了一系列变体。您将看到,您可以将开关
与函数
结合使用。它们不是相互排斥的。
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)];
}
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)];
}