我一直在尝试从字母数字字符中创建随机字符串,并使用Do-While循环检查随机字符串是否满足要求。否则,它应该继续执行Do Loop。但是,我发现我一直在反复生成同一字符串的代码。为了演示,我设置了int message_length = 2
并要求While循环检查生成的字符串是否像“ while (check != "ab")
”那样是“ ab”。
我已经尝试将Random seed = new Random()
放入Do循环中,Do循环中以及Main()中,您可以看到我注释掉的代码。他们都没有工作。我使用了Visual Studio 2017 Windows 10 Home。有人可以帮忙吗?非常感谢!
(进一步说明:尽管我现在使代码工作正常,但我仍然不明白为什么在这种情况下原始行check = all_message.ToString();
会中断随机数生成器。恕我直言,while条件{{1} }仍为True,因此循环将继续进行,但是为什么随机数生成器停止生成新种子呢?
while (check != "ab")
答案 0 :(得分:1)
您将在循环的每次迭代中重置计数器“ i”,因此您将始终将生成的列表的第一个元素写入控制台。在循环外初始化我。
答案 1 :(得分:1)
在其他人的帮助下,我找到了问题所在。关键更改是check = string.Join("", all_message.ToArray());
。除了其他一些小的更改,解决方案在这里:
using System;
using System.Text;
using System.Collections.Generic;
public class Program
{
//static Random seed = new Random();
public static void Main(string[] arggs)
{
Random seed = new Random();
const string src = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
Console.WriteLine(src.Length);
int string_length = 2;
string check = "";
int i = 0;
do
{
//Random seed = new Random();
List<string> all_message = new List<string>();
StringBuilder message = new StringBuilder();
for (int j = 0; j < string_length; j++)
{
char c = src[seed.Next(0, src.Length)];
message.Append(c);
}
all_message.Add(message.ToString());
check = string.Join("", all_message.ToArray());
//Console.WriteLine(check);
Console.WriteLine(i + " = " + check);
i++;
}
while (check != "ab");
}
}
答案 2 :(得分:0)
我运行了以下内容:
Random seed = new Random();
const string src = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
Console.WriteLine(src.Length);
int string_length = 2;
List<string> all_message = new List<string>();
string check = "";
int i = 0;
do
{
//Random seed = new Random();
StringBuilder message = new StringBuilder();
for (int j = 0; j < string_length; j++)
{
char c = src[seed.Next(0, src.Length)];
message.Append(c);
}
all_message.Add(message.ToString());
check = all_message.ToString();
Console.WriteLine(i + " = " + all_message[i]);
i++;
}
while (i < 3);
我的输出是:
62
0 = PQ
1 = xw
2 = he
请注意,索引i
是在循环外部初始化的,因此不会每次都重置。
问题就变成了,您的要求是什么,何时终止循环,check
应该是什么?
答案 3 :(得分:0)
提示:阅读评论。
Random seed = new Random();
const string src = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
Console.WriteLine(src.Length);
int string_length = 2;
List<string> all_message = new List<string>();
string check = "";
do
{
//Random seed = new Random(); -- you do not need it any more hier.
int i = 0; // if you whant counting with i, put it above the do while.
StringBuilder message = new StringBuilder();
for (int j = 0; j < string_length; j++)
{
char c = src[seed.Next(0, src.Length)];
message.Append(c);
}
all_message.Add(message.ToString());
check = all_message.ToString(); // this is the list from string you do not need it realy and list to string = System.Collections.Generics.List....
//instead check should be equal to all_message[i] case i counting properly
Console.WriteLine(i + " = " + all_message[i]); // and hier simply print the check, you allready have the value.
i++;
}
while (check != "ab");