
[英]C# Client/Server socket will not connect again after it has been disconnected
[英]C#: Is there a way to stop a random from generating again after it has been displayed
我需要一种方法来存储括号中为真/假或 if 语句生成的随机数,但不能,因为它在我输入答案后不断重新生成新数字。 此外,如果它生成了 44 个随机数,而我回答了 48 个随机数,我该如何调用随机数来显示它是否正确? 只是一个简单的练习来帮助自己进行 C# 编码。 感谢您提供的任何答案。
//编码根本没有任何问题。 问题是我使用的在线编译器,每次我输入答案时它都会不断刷新,所以我认为它在 for 循环中打印了整个集合后,它会给我一组新的随机数字。 我为造成的混乱道歉! 我没有视觉工作室,所以我使用的是在线编译器。
public static void Main()
{
Random rand = new Random();
int count, a, b, x, y;
Console.Write("Sequence: ");
count = int.Parse(Console.ReadLine());
Console.Write("Minima: ");
x = int.Parse(Console.ReadLine());
Console.Write("Maxima: ");
y = int.Parse(Console.ReadLine());
int[] roll = new int[count];
for(a = 0; a < count; a++){
roll[a] = rand.Next(x, y);
}
Console.Write("( ");
foreach(var nr in roll){
Console.Write(nr + " ");
}
Console.WriteLine(")");
Console.WriteLine("");
int[] answer = new int[count];
for(b = 0; b < count; b++){
Console.Write("{0}. Answer: ",b+1);
answer[b] =int.Parse(Console.ReadLine());
}
Console.ReadLine();
}
}
解决办法是这样
for(int i = 0; i < count; i++)
{
if(roll[i] == answer[i])
{
Console.WriteLine("Question " + i + " is correct");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.