簡體   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