代码首先询问代码的位数。然后它会创建一个与答案一样长的随机字母串,并开始循环遍历字符,直到找到代码。每个匹配的字符都保存在同一个位置,直到每个字符匹配为止。
问题在于控制台不断猜测错误的字符,导致控制台继续猜测错误的字符并且从未解决它的情况。
所以,我希望控制台在猜到错误字符后不要尝试它们。
附带说明一下,如果有人对我如何更改代码以一次猜测一个字符有任何想法,然后继续下一个,请告诉我,谢谢。
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);
}
}
烙印99
相关分类