简体   繁体   English

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

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

I have a set of randomly generated incorrect string(words) that appears to a user and I want to evaluate the user suggestion or input with the correct word. 我有一组随机生成的不正确的字符串(单词)出现在用户面前,我想评估用户的建议或输入正确的单词。 I can't find my way out on how to match the user's input to the correct word, someone helping me would really be relieving. 对于如何将用户输入内容与正确的单词相匹配,我找不到出路,有人会帮助我。

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

Store the random number generated and use in your processes. 存储生成的随机数,并在您的流程中使用。

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

As mentioned in comments you should summarize your code to avoid redundant code and make it more readable. 如注释中所述,您应该总结代码以避免冗余代码,并使代码更具可读性。 And of course you should save the random number in a variable to avoid that you get new number in ever if. 当然,您应该将随机数保存在变量中,以免在任何情况下都获得新的数。

If with logical or operator 如果具有逻辑或运算符

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 the Length of words[] are going to increase you'll just have to add a single line to this if and the job is done here. 如果words[]Length要增加,则只要在此处添加一行就可以了。

Another way would be a dictionary or list or something similar I guess. 另一种方法是字典或列表或类似的东西。

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

In that case you dont't even have to write more code than the new word inside the dictionary. 在这种情况下,您甚至不必编写比字典中新单词更多的代码。

You just need to do this: 您只需要这样做:

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

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

Otherwise you're always calling next on random so its probable you won't be comparing the correct string. 否则,您总是随机调用next,因此可能不会比较正确的字符串。

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

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