这是输出1或0的代码,具体取决于2个数组的相等性
static void Main(string[] args)
{
while (true)
{
Console.WriteLine(Here());
}
这就是魔术发生的地方。
static int Here()
{
Random rnd = new Random();
PlayerInput();
int[] intarray = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
string[] sarray = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
int i = rnd.Next(10); // creates a number between 1 and 10
int x = rnd.Next(10);
string iarray = intarray[x].ToString();
if (iarray == sarray[i])
{
return 1;
}
else
{
return 0;
}
}
当两个数组不相等时输出0,反之亦然。现在我想计算它输出的次数0& 1。
问题:
我怎么能这样做?
我应该将输出传输到数组以便于操作吗?
答案 0 :(得分:3)
除非我误解你的问题,否则你似乎只能有两个反变量:
static void Main(string[] args)
{
int totalOnes = 0;
int totalZeroes = 0;
while (true) // need to replace this with something that will actually exit!!
{
int ret = Here();
if (ret == 1) totalOnes++; else totalZeroes++;
Console.WriteLine(ret);
}
Console.WriteLine("Total Ones: {0} Total Zeroes: {1}", totalOnes, totalZeroes);
}
编辑:感谢L J指出您的while
循环永远不会退出,因为您有while (true)
。你需要解决这个问题。
答案 1 :(得分:0)
static void Main(string[] args)
{
var zeroCount = 0;
var oneCount = 0;
while (true)
{
var result = Here();
if (result == 1) oneCount++;
if (result == 0) zeroCount++;
Console.WriteLine($"Actual result {result}, zero count {zeroCount}, One count {oneCount}");
}
}
答案 2 :(得分:-1)
static int Here()
{
Random rnd = new Random();
PlayerInput();
int[] intarray = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
string[] sarray = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
**var Count1 = 0;
var Count0 = 0;**
int i = rnd.Next(10); // creates a number between 1 and 10
int x = rnd.Next(10);
string iarray = intarray[x].ToString();
if (iarray == sarray[i])
{
**Count1++;**
return 1;
}
else
{
**Count0++;**
return 0;
}
}
Count0将包含输出0的次数。
Count1将包含输出1的次数。