Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 评估用户';用一组随机生成的字符串值在c中输入#_C# - Fatal编程技术网

C# 评估用户';用一组随机生成的字符串值在c中输入#

C# 评估用户';用一组随机生成的字符串值在c中输入#,c#,C#,我有一组随机生成的不正确字符串(单词),出现在用户面前,我想用正确的单词评估用户的建议或输入。我找不到如何将用户的输入与正确的单词相匹配的方法,有人帮助我真的很轻松 using System; class Program { static void Main(string[] args) { Random Rnd = new Random(); string[] words = { "M_R_", "I_R_E_" }; Cons

我有一组随机生成的不正确字符串(单词),出现在用户面前,我想用正确的单词评估用户的建议或输入。我找不到如何将用户的输入与正确的单词相匹配的方法,有人帮助我真的很轻松

using System;

class Program
{
    static void Main(string[] args)
    {
        Random Rnd = new Random();
        string[] words = { "M_R_", "I_R_E_" };
        Console.WriteLine(words[Rnd.Next(0, words.Length)]);


        Console.WriteLine("Please enter your name");
        string name = Console.ReadLine();

        int points = 0;
        if ((words[Rnd.Next(0, words.Length)]).Equals("I_R_E_") && name == "Israel")
        {
            points += 5;

            Console.WriteLine("Marks: {0}", points);
            Console.WriteLine("You won!!!!");
        }
        else if ((words[Rnd.Next(0, words.Length)]).Equals("M_R_") && name == "Mark")
        {
            points += 5;

            Console.WriteLine("Marks: {0}", points);
            Console.WriteLine("You won!!!!");
        }
        else
        {
            Console.WriteLine("Incorrect");
        }
        Console.ReadLine();
    }
}

您只需执行以下操作:

var word = words[Rnd.Next(0, words.Length)];
Console.WriteLine(word);
...

if (word.Equals("I_R_E_") && name == "Israel")
...

否则,您总是随机调用next,因此可能无法比较正确的字符串

存储生成的随机数并在流程中使用

        Random Rnd = new Random();
        string[] words = { "M_R_", "I_R_E_" };
        var randomNumber = Rnd.Next(0, words.Length);
        Console.WriteLine(words[randomNumber]);


        Console.WriteLine("Please enter your name");
        string name = Console.ReadLine();

        int points = 0;
        if ((words[randomNumber]).Equals("I_R_E_") && name == "Israel")
        {
            points += 5;

            Console.WriteLine("Marks: {0}", points);
            Console.WriteLine("You won!!!!");
        }
        else if ((words[randomNumber]).Equals("M_R_") && name == "Mark")
        {
            points += 5;

            Console.WriteLine("Marks: {0}", points);
            Console.WriteLine("You won!!!!");
        }
        else
        {
            Console.WriteLine("Incorrect");
        }

正如在注释中提到的,您应该总结代码,以避免冗余代码并使其更具可读性。当然,你应该将随机数保存在一个变量中,以避免在ever if中得到新的数

如果使用逻辑or运算符

Random Rnd = new Random();
string[] words = { "M_R_", "I_R_E_" };
int points = 0;
int randomNumber = Rnd.Next(0, words.Length);

Console.WriteLine(randomNumber);  

Console.WriteLine("Please enter your name");
string name = Console.ReadLine();

var word = words[randomNumber];

if((word.Equals("I_R_E_") && name == "Israel")
|| (word.Equals("M_R_") && name == "Mark")) // you can add words here
{
    points += 5;
    Console.WriteLine("Marks: {0}\nYou won!!!!", points);
}
else
{
    Console.WriteLine("Incorrect");
}
如果
单词[]
长度将增加,您只需在该If中添加一行,即可完成此任务

另一种方法是字典或列表或类似的东西

Dictionary<int, string> words = new Dictionary<int, string>()
{
    {0, "Israel"},
    {1, "Mark"},
    {2, "Foo"},
    {3, "Bar"} // add more if you want
};

Random Rnd = new Random();
int randomNumber = Rnd.Next(0, words.Count); // Attention! Dict, list etc. use .Count not .Length!
int points = 0;

Console.WriteLine(randomNumber);  

Console.WriteLine("Please enter your name");
string name = Console.ReadLine();    

string word = "";
if(!words.TryGetValue(randomNumber, out word)) return; // whoops index not found in dictionary!

if(word.Equals(name))
{
    points += 5;
    Console.WriteLine("Marks: {0}\nYou won!!!!", points);
}
else
    Console.WriteLine("Incorrect");
字典单词=新字典()
{
{0,“以色列”},
{1,“标记”},
{2,“Foo”},
{3,“Bar”}//如果需要,可以添加更多
};
随机Rnd=新随机();
int randomNumber=Rnd.Next(0,words.Count);//注意!口述、列表等。使用。计数而不是。长度!
积分=0;
控制台写入线(随机数);
Console.WriteLine(“请输入您的姓名”);
字符串名称=Console.ReadLine();
字串=”;
if(!words.TryGetValue(randomNumber,out word))返回;//在字典中找不到索引!
if(单词等于(名称))
{
积分+=5分;
WriteLine(“分数:{0}\n您赢了!!!!”,点数);
}
其他的
控制台。写入线(“不正确”);

在这种情况下,你甚至不必编写比字典中的新词更多的代码。

请注意,每次调用Rnd.Next都会创建一个新的随机数。如果你打算添加越来越多的词[],你应该使用开关大小写。开关(Rnd.Next(0,words.Length)]{case 0:break;case 1:break;}等等。否则,您可以在if中使用逻辑OR来避免冗余代码。如果((words[Rnd.Next(0,words.Length)].Equals((words[Rnd.Next(0,words.Length)]).Equals((words[Rnd.Next(0,words.Length)])((words[Rnd.Next(0,words.Length)]){}@Nitro:请将您的评论放入答案中,解释解决方案并重新格式化代码。“那太好了!”彼得说,希望你没问题