Anagram解码器正在添加到前一个字符串

时间:2018-04-06 11:19:53

标签: c# string loops anagram

所以我制作了一个Anagram解码器,基本上,它通过,选择一个独特版本的anagram,然后将它添加到列表并清除它。这样,当它再次运行时,如果它试图创建该单词,它会循环回来并再次尝试。但是,当它结束时,它会添加到之前的string

我的意思是,如果string是"嘿",它第一次可以做到#34; yeh",第二次它" d" #34; yehhye&#34 ;.它将新的一个添加到最后一个的末尾。这是没有意义的,因为我清除了存储它的变量(除了存储之前字谜的那个)。

以下是代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Anagram_Decoder {
    class Program {
        static void Main(string[] args) {
            Random random = new Random();
            // Create string, count it, and split it
            string anagram = "hey";
            int anagramLength = anagram.Length;
            int result = anagramLength;

            // Find possibilities
            for (int i = 1; i < anagramLength; i++) {
                result = result * i;
            }

            int anagramPossibilities = result;
            List<string> anagramsFound = new List<string>();
            char[] anagramSplit = anagram.ToCharArray();
            int[] numbers = new int[anagram.Length];
            for (int l = 0; l < anagramPossibilities; l++) {
                string endResult = "";
                bool failed = false;
                while (true) {
                    numbers = new int[anagram.Length];
                    for (int i = 0; i < anagram.Length; i++) {
                        while (true) {
                            System.Threading.Thread.Sleep(15);
                            int randomNumber = random.Next(1, anagram.Length + 1);
                            foreach (int n in numbers) {
                                if (n == randomNumber && failed == false) {
                                    failed = true;
                                }
                            }
                            if (failed == true) {
                                failed = false;
                                continue;
                            }
                            else {
                                numbers[i] = randomNumber;
                                endResult += randomNumber;
                                break;
                            }
                        }
                    }
                    numbers = new int[anagram.Length];
                    failed = false;
                    char[] endResultChars = endResult.ToCharArray();
                    string finalResult = "";
                    foreach (char i in endResultChars) {
                        finalResult += anagram[Convert.ToInt32(Convert.ToString(i)) - 1];
                    }
                    foreach (string n in anagramsFound) {
                        if (n == finalResult && failed == false) {
                            failed = true;
                        }
                    }
                    if (failed == true) {
                        failed = false;
                        continue;
                    }
                    else {
                        anagramsFound.Add(finalResult);
                        finalResult = "";
                        Console.ReadKey();
                    }
                }
            }
            Console.ReadKey();
        }
    }
}

0 个答案:

没有答案