C#需要用switch语句添加随机响应
C# need to add random response with switch statement
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
}
}
}
}
我需要我的 correct== true 和 false 来在 4 个可能的选择中随机响应。我需要通过执行 switch 语句来发出每个响应来做到这一点。也通过使用随机 select 哪个响应出现。
很好!
太棒了!
干得好!
继续努力!
还有 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;
}
试试这个:
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)];
或者,使用 switch
和 Func
:
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);
或作为 "normal" 函数:
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)];
}
或作为 "normal" 函数,但没有 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)];
}
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
}
}
}
}
我需要我的 correct== true 和 false 来在 4 个可能的选择中随机响应。我需要通过执行 switch 语句来发出每个响应来做到这一点。也通过使用随机 select 哪个响应出现。
很好!
太棒了!
干得好!
继续努力!
还有 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;
}
试试这个:
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)];
或者,使用 switch
和 Func
:
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);
或作为 "normal" 函数:
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)];
}
或作为 "normal" 函数,但没有 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)];
}