इसलिए मैंने सी # में एक बहुत ही सरल शब्द जनरेटर प्रोग्राम बनाया जो अपेक्षाकृत अच्छी तरह से काम करता है। यह उपयोगकर्ता द्वारा परिभाषित लंबाई के आधार पर एक शब्द उत्पन्न करता है।यादृच्छिक शब्द जनरेटर # 2
एल्गोरिथ्म एक व्यंजन और फिर दृश्य है, जो आदर्श नहीं है में प्रत्येक लगातार पत्र के लिए एक स्वर कहते हैं, लेकिन बुनियादी शब्द के लिए काफी अच्छी तरह से काम करता है।
मेरा एकमात्र मुद्दा यह है कि मैंने इसे "यू" अक्षर अनुक्रम में जोड़ने के लिए कहा था, यदि कोई "क्यू" इससे ठीक पहले दिखाई देता है, लेकिन इससे कोई फर्क नहीं पड़ता कि मैंने यह किया है कि यह कम से कम 1 अक्षर भी बनाता है लंबा।
मैं इसे ऊपर टिप्पणी में एक स्टार के साथ मेरी समस्या क्षेत्र चिह्नित किया है। यहाँ कोड है:
public void WordFinder()
{
string word = null;
int cons;
int vow;
//counter
int i = 0;
bool isword = false;
Random rnd = new Random();
//set a new string array of consonants
string[] consonant = new string[]{"b","c","d","f","g","h","j","k","l","m","n","p","q","r","s","t","v","w","x","y","z"};
//set a new string array of vowels
string[] vowel = new string[]{"a","e","i","o","u"};
while (isword == false)
{
word = null;
Console.WriteLine("Pick the length of a word");
int num = Convert.ToInt32(Console.ReadLine());
//set the counter "i" to 1
i = 1;
if (num%2 == 0)
{
while (i <= num)
{
if (num != 1)
{
// current consonant = random consonant
cons = rnd.Next(0, 20);
// get the consonant from the string array "consonant" and add it to word
word = word + consonant[cons];
// add 1 to counter
i ++;
//* if the consonant is "q"
if (cons == 12)
{
// add "u" right after it
word = word + vowel[4];
// add 1 to counter
i++;
}
}
vow = rnd.Next(0, 4);
word = word + vowel[vow];
i ++;
}
}
if (num % 2 != 0)
{
while (i <= num - 1)
{
//repeat same code as done to even length
if (num != 1)
{
cons = rnd.Next(0, 20);
word = word + consonant[cons];
i ++;
if (cons == 12)
{
word = word + vowel[4];
i ++;
}
}
vow = rnd.Next(0, 4);
word = word + vowel[vow];
i ++;
}
// if the length is not equal to 1
if (num != 1)
{
// add a consonant to the end of the word
cons = rnd.Next(0, 20);
word = word + consonant[cons];
}
//if the length is 1
else if (num == 1)
{
// pick a vowel
vow = rnd.Next(0, 4);
word = word + vowel[vow];
}
}
i = 1;
Console.WriteLine(word);
Console.WriteLine("Is this a word? (y/n)");
string q = Console.ReadLine();
q = q.ToLower();
//if the answer is yes, then it is a word and end the loop
if (q == "y" || q == "yes")
{
isword = true;
}
//if the answer is no try the loop again
else if (q == "n" || q == "no")
{
isword = false;
}
}
}
// main method
static void Main(string[] args)
{
Program prog = new Program();
prog.WordFinder();
//wait for user input
Console.ReadLine();
}
}
उस कोड में लगभग सभी टिप्पणियां सहायक नहीं हैं। टिप्पणियां आपको यह बताने के लिए वहां होनी चाहिए कि कोड * आपको * नहीं बताता (या यह कोड से स्पष्ट नहीं है)। एक टिप्पणी जो सिर्फ बताती है कि कोड क्या करता है, किसी की भी मदद नहीं करता है, यह सिर्फ लोगों के समय को बर्बाद कर देता है। – Servy
मुझे समझ में नहीं आता कि * उसने शब्द को एक पत्र (या "क्यू" को "यू" लंबा * मतलब के बाद जोड़ा गया था। आप 'व्यंजन [विपक्ष] ==' q 'का उपयोग करने का प्रयास कर सकते हैं। 'आपकी' if' स्थिति के रूप में, आपके पास जादू संख्या 12 की जगह है। –
जिस तरह से मैं इसे पढ़ रहा हूं, लूप के माध्यम से प्रत्येक पास के लिए आप 2 अक्षर जोड़ रहे हैं। एक व्यंजन और स्वर। उत्पन्न करने के मामले में एक 'क्यू' आप कुल 3 अक्षरों को जोड़ रहे हैं: 'QU' प्लस एक यादृच्छिक अन्य स्वर। यहां तर्क दिखाई देता है। – NotMe