Reputation: 25
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();
}
}
Upvotes: 1
Views: 79
Reputation: 851
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.
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.
Upvotes: 1
Reputation: 3009
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");
}
Upvotes: 1
Reputation: 4679
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.
Upvotes: 0