简体   繁体   English

尝试在随机生成 1 或循环 10 次后结束 while 循环

[英]Trying to end a while loop once a 1 is randomly generated or it loops 10 times

(I apologize for the atrocious formatting) (我为残暴的格式道歉)

I am attempting to make a random number generator (min = 1, max = 10) that loops until either我正在尝试制作一个随机数生成器(最小 = 1,最大 = 10),循环直到

a) a 1 is generated (serve as primary sentinel). a) 生成 1(作为主要哨兵)。 b) loop iterates 10 times (secondary sentinel) without generating 1. b) 循环迭代 10 次(二级哨兵)而不生成 1。

I am looking for something like this:我正在寻找这样的东西:

6 3 8 4 1 "A one was received in 5 attempts," 6 3 8 4 1 “5 次尝试收到一个,”

or或者

3 8 5 7 2 2 4 8 8 3 "A one was never received." 3 8 5 7 2 2 4 8 8 3 “从未收到过。”

but the problem is that the loop continues even if a 1 is generated, and the "A one was never recieved."但问题是即使生成了 1 循环也会继续,并且“从未收到过 A”。 message displays.消息显示。 The loop is as follows:循环如下:

while (randomNumber.Next() != 1 || loopCounter <= 10));
{
    loopCounter++;
    Console.WriteLine(randomNumber.Next(10));
    Thread.Sleep(100);
    if (randomNumber.Next() == 1)
    {
        Console.WriteLine($"A one was recieved in {loopCounter} attempts.")
        break;
    }
    if (loopCounter <= 10)
    {
        Console.WriteLine("A one was not recieved.")
        break;
    }
}

giving something like给予类似的东西

1 4 6 9 7 1 3 3 1 9 "A one was not recieved." 1 4 6 9 7 1 3 3 1 9 “一个没有收到。”

You more or less had the right idea for deducting whether each of the two conditions were met (aside from the sleeping thread ?).您或多或少有正确的想法来推断是否满足两个条件中的每一个(除了睡眠线程?)。

But if you do it after initialising variables to track what you were doing rather than checking as you go along it's easier to produce a method that meets the requirements for what I'm assuming is a homework question.但是,如果您在初始化变量以跟踪您正在做的事情之后这样做,而不是在您进行时进行检查,则更容易生成满足我假设的家庭作业问题要求的方法。

Random rand = new Random();
int randomNumber = 0;

int maxLoops = 10;
string numbersSoFar = "";

for (int i = 1; i <= maxLoops; i++)
{
    //Generate random number between 1 and 10:
    randomNumber = rand.Next(1, 10);
    numbersSoFar += randomNumber + " ";
    
    if (randomNumber == 1)
    {
        Console.WriteLine(numbersSoFar);
        Console.WriteLine("1 was found on loop number: " + i);
        break;
    }
    else if (i == 10)
    {
        Console.WriteLine(numbersSoFar);
        Console.WriteLine("10 loops completed, no 1 found.");
    } 
}

Since one of the conditions is that you have a maximum number of loops, you can use this in a for loop , rather than a while loop giving you access to an immediate iterator to compare against rather than declaring an extra value coupled with a while loop.由于其中一个条件是您有最大数量的循环,您可以在for loop使用它,而不是在 while 循环中使用它,让您可以访问立即iterator进行比较,而不是声明与 while 循环相结合的额外值. This way it terminated automatically where as in your question you'd need a breakout/termination clause additionally for that.这样它就会自动终止,就像你的问题一样,你需要额外的突破/终止条款。

Your main issue is that Next() returns a new random integer on each call, and without arguments returns any valid integer, so you have about a 1/(2^32) chance of getting 1. Instead you should be calling it once on each iteration and reading that result a few times.您的主要问题是Next()在每次调用时返回一个新的随机整数,并且没有参数返回任何有效整数,因此您大约有 1/(2^32) 的机会获得 1。相反,您应该调用它一次每次迭代和读取结果几次。 Your loop could look something like this:您的循环可能如下所示:

List<int> nums = new List<int>();
for(int i = 0; i < 10; ++i)
{
    nums.Add(randomNumber.Next(1, 10));
    if(nums.Last() == 1)
    {
        break;
    }
}
Console.Write(String.Join(" ", nums) + " "); 
if(nums.Last() == 1)
{
    Console.WriteLine("A 1 was received after " + nums.Length + " attempts");
}
else
{
    Console.WriteLine("A 1 was not received.");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM