【发布时间】:2017-03-06 08:44:05
【问题描述】:
这是根据 2 个数组的相等性输出 1 或 0 的代码
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,反之则输出 1。现在我想计算它输出 0 和 1 的次数。
问题:
我该怎么做?
是否应该将输出传输到数组以便于操作?
【问题讨论】:
-
你什么时候打破你的while循环?
-
你熟悉 lamda 表达式吗?您可以使用以下
.GroupBy, .Where, and .Select轻松做到这一点,如果您需要查看工作示例,我可以在 List中发布使用非重复项和重复项的示例 -
您的
while循环应该如何退出?永远不要使用while(true),除非你真的,真的知道你在做什么。这将无限循环。 -
另外,你为什么要将
ints转换为Strings来比较它们?只需将它们与int值进行比较
标签: c# boolean logic console-application