你能帮我解决两个或一个问题吗,不确定。我正在尝试从多个 private static string
获取字符串和整数值,这些字符串包含不同的进程,每个进程都有一个不同类型的输出,我想返回以作为函数顺序调用,以便进一步组合。
首先我想说我不知道,如果下面的方式是错误的,我不知道如何创建这样的函数,它可以返回值以获得期望的结果,并且很高兴看到这种情况的一些有用的例子,但是什么我想在这里做,给我两个问题:
如果我想调用 b1
,然后调用 b2
,然后再调用 b1
,我想从每一个中获得唯一的计算结果,但现在我得到了相等的每个结果,如果我使用如下所示的随机数,但我怀疑在这种情况下,这个问题的原因是在 private static string
中错误地使用了 random,还需要弄清楚,如何在这种情况下正确使用它。
结果是这样的:
23 23 23
但我认为这不仅是一个相同的主要问题,因为如果我返回一些不同的计算结果,例如从私有(private)静态字符串 b1 和 b2 中分别从不同列表的不同字符串集中选择,结果只是按照顺序调用的顺序重复处理两次或多次返回相同的值。我不能肯定地说,但它看起来不像问题的相同原因,可以在上面。因为它看起来像这样:
77 34 77
或者为了更清楚,如果我通过计算表单字符串选择一些单词,例如:“hello world, how are you” 并且在每个 private static string
中我进行不同的计算以从此字符串或不同的字符串中获取单个单词,我得到了相同的结果或正确地说出相同的结果两次调用相同的过程,就像重复结果而不是单独调用它一样:
world you world
下面的示例仅显示随机数的情况,因为如果上一个问题不同并且与使用错误的随机数无关,则原因必须是一般方法,我应该以不同的方式解决这个问题。
class Program
{
static void Main(string[] args)
{
string a1 = b1();
string a2 = b2();
string a3 = b1();
string comb = (a1 + a2 + a3);
Console.WriteLine(comb);
Console.ReadLine();
}
private static string b1()
{
Random random = new Random();
int ran1 = random.Next(1, 100);
return ran1;
}
private static string b2()
{
Random random = new Random();
int ran2 = random.Next(1, 100);
return ran2;
}
}
编辑 1:
通过随机数或任何其他方式选择字符串返回的示例:
static void Main(string[] args)
{
string a1 = b1();
string a2 = b2();
string a3 = b1();
string comb = (a1 + a2 + a3);
Console.WriteLine(comb);
Console.ReadLine();
}
private static string b1()
{
Dictionary<int, string> mass1 = new Dictionary<int, string>()
{ { 1, "A" }, { 2, "B" }, { 3, "C" }};
Random random1 = new Random();
int rndCase2 = random1.Next(1, 4);
string key1;
mass1.TryGetValue(rndCase2, out key1);
return key1;
}
private static string b2()
{
Dictionary<int, string> mass2 = new Dictionary<int, string>()
{ { 1, "E" }, { 2, "F" }, { 3, "G" }};
Random random2 = new Random();
int rndCase2 = random2.Next(1, 4);
string key2;
mass2.TryGetValue(rndCase2, out key2);
return key2;
}
结果是:
AEA
编辑 2:(已解决)
class Program
{
private static Random random = new Random();
static void Main(string[] args)
{
string a1 = b1();
string a2 = b2();
string a3 = b1();
string comb = (a1 + a2 + a3);
Console.WriteLine(comb);
Console.ReadLine();
}
private static string b1()
{
Dictionary<int, string> mass1 = new Dictionary<int, string>()
{ { 1, "A" }, { 2, "B" }, { 3, "C" }};
return mass1[random.Next(1, 4)];
}
private static string b2()
{
Dictionary<int, string> mass2 = new Dictionary<int, string>()
{ { 1, "E" }, { 2, "F" }, { 3, "G" }};
return mass2[random.Next(1, 4)];
}
}
最佳答案
仅使用一个随机
实例
class Program
{
static void Main(string[] args)
{
Random random = new Random();
string a1 = b1(random);
string a2 = b2(random);
string a3 = b1(random);
string comb = (a1 + a2 + a3);
Console.WriteLine(comb);
Console.ReadLine();
}
private static string b1(Random random)
{
int ran1 = random.Next(1, 100);
return ran1.ToString();
}
private static string b2(Random random)
{
int ran2 = random.Next(1, 100);
return ran2.ToString();
}
}
编辑1
这同样适用于您的其他 b1()
和 b2()
方法:
private static string b1(Random random)
{
Dictionary<int, string> mass1 = new Dictionary<int, string>()
{ { 1, "A" }, { 2, "B" }, { 3, "C" }};
return mass1[random.Next(1, 4)];
}
private static string b2(Random random)
{
Dictionary<int, string> mass2 = new Dictionary<int, string>()
{ { 1, "E" }, { 2, "F" }, { 3, "G" }};
return mass2[random.Next(1, 4)];
}
而 Main()
与我之前的编辑保持相同
关于c# - 如何从函数返回值以在每一行用新结果顺序调用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40501757/