繁体   English   中英

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

[英]Evaluating a user's input with a set of randomly generated string values in c#

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

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();
    }
}

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

        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");
        }

如注释中所述,您应该总结代码以避免冗余代码,并使代码更具可读性。 当然,您应该将随机数保存在变量中,以免在任何情况下都获得新的数。

如果具有逻辑或运算符

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");
}

如果words[]Length要增加,则只要在此处添加一行就可以了。

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

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");

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

您只需要这样做:

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM