第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

強制控制臺在隨機循環(huán)的代碼猜測器中跳過已經(jīng)猜測的字符

強制控制臺在隨機循環(huán)的代碼猜測器中跳過已經(jīng)猜測的字符

C#
嗶嗶one 2022-07-10 10:11:07
代碼首先詢問代碼的位數(shù)。然后它會創(chuàng)建一個與答案一樣長的隨機字母串,并開始循環(huán)遍歷字符,直到找到代碼。每個匹配的字符都保存在同一個位置,直到每個字符匹配為止。問題在于控制臺不斷猜測錯誤的字符,導致控制臺繼續(xù)猜測錯誤的字符并且從未解決它的情況。所以,我希望控制臺在猜到錯誤字符后不要嘗試它們。附帶說明一下,如果有人對我如何更改代碼以一次猜測一個字符有任何想法,然后繼續(xù)下一個,請告訴我,謝謝。using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;namespace ConsoleApp3{    class Program    {        private static Random random = new Random();        public static string RandomString(int length)        {            const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";            return new string(Enumerable.Repeat(chars, length)              .Select(s => s[random.Next(s.Length)]).ToArray());        }        static void Main(string[] args)        {            Console.ForegroundColor = ConsoleColor.Green;            string intro6 = "How many characters in the password? (USE INTEGERS)";            foreach (char c in intro6)            {                Console.Write(c);                Thread.Sleep(50);            }            Console.WriteLine("");            string delta = Console.ReadLine();            try            {                int passwordlength = Convert.ToInt32(delta);                // BARRIER                string password = RandomString(passwordlength);                Random r = new Random();                string letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";                List<string> dictionary = new List<string>(new string[] {            password        });                string word = dictionary[r.Next(dictionary.Count)];                List<int> indexes = new List<int>();                Console.ForegroundColor = ConsoleColor.Red;                StringBuilder sb = new StringBuilder();                for (int i = 0; i < word.Length; i++)                {                    sb.Append(letters[r.Next(letters.Length)]);                    if (sb[i] != word[i])                    {                        indexes.Add(i);                    }                }
查看完整描述

1 回答

?
烙印99

TA貢獻1829條經(jīng)驗 獲得超13個贊

正如 wubbler 在他的評論中提到的那樣,用于創(chuàng)建隨機密碼的字符與用于猜測密碼的字符之間似乎存在差異。


去創(chuàng)造:


const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

猜測:


string letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

當隨機生成的密碼包含任何數(shù)字時,這會導致程序無法猜測密碼。通過將數(shù)字添加到可猜測的字符中,程序會更加一致地成功。


至于請求:


我希望控制臺在猜到錯誤字符后不要嘗試它們


您可以通過為每個索引保留一個可猜測字符的集合來實現(xiàn)此目的,然后在猜測到給定索引后從它的集合中刪除一個字符。下面的代碼滿足了這一點:


static void Main(string[] args)

{

    Console.ForegroundColor = ConsoleColor.Green;


    string intro6 = "How many characters in the password? (USE INTEGERS)";

    foreach (char c in intro6)

    {

        Console.Write(c);

        Thread.Sleep(50);

    }

    Console.WriteLine("");

    string delta = Console.ReadLine();


    try

    {

        int passwordlength = Convert.ToInt32(delta);


        // BARRIER


        string password = RandomString(passwordlength);


        Random r = new Random();

        string letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

        List<string> dictionary = new List<string>(new string[] { password });


        string word = dictionary[r.Next(dictionary.Count)];

        List<int> indexes = new List<int>();

        Console.ForegroundColor = ConsoleColor.Red;

        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < word.Length; i++)

        {

            sb.Append(letters[r.Next(letters.Length)]);

            if (sb[i] != word[i])

            {

                indexes.Add(i);


            }

        }

        Console.WriteLine(sb.ToString());


        var charsToGuessByIndex = indexes.ToDictionary(k => k, v => letters);


        while (indexes.Count > 0)

        {

            int index;


            Thread.Sleep(10);

            Console.Clear();


            for (int i = indexes.Count - 1; i >= 0; i--)

            {

                index = indexes[i];


                var charsToGuess = charsToGuessByIndex[index];

                sb[index] = charsToGuess[r.Next(charsToGuess.Length)];

                charsToGuessByIndex[index] = charsToGuess.Remove(charsToGuess.IndexOf(sb[index]), 1);

                if (sb[index] == word[index])

                {

                    indexes.RemoveAt(i);

                }

            }

            var output = sb.ToString();


            for (int i = 0; i < output.Length; i++)

            {

                if (indexes.Contains(i))

                {

                    Console.ForegroundColor = ConsoleColor.Red;

                }

                else

                {

                    Console.ForegroundColor = ConsoleColor.Cyan;

                }


                Console.Write(output[i]);

            }


            Console.WriteLine();

        }


        Console.ForegroundColor = ConsoleColor.Green;


        string outro1 = "Password successfully breached. Have a nice day.";

        foreach (char c in outro1)

        {

            Console.Write(c);

            Thread.Sleep(20);

        }

        Console.WriteLine("");

        Thread.Sleep(100);


        Console.ReadLine();

    }

    catch

    {

        if (delta is string)

        {

            Console.ForegroundColor = ConsoleColor.Red;

            Console.Clear();

            Console.WriteLine("FATAL ERROR PRESS ENTER TO EXIT");



            Console.ReadLine();

        }

        else

        {

            Console.WriteLine("welp, it was worth a try.");

            Console.ReadLine();

        }

    }

}

charsToGuessByIndex跟蹤每個索引可以猜測哪些字符,并在猜測字符的 for 循環(huán)內(nèi)相應地更新:


charsToGuessByIndex[index] = charsToGuess.Remove(charsToGuess.IndexOf(sb[index]), 1);


查看完整回答
反對 回復 2022-07-10
  • 1 回答
  • 0 關注
  • 111 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號