簡體   English   中英

循環故障時,隨機積分器僅在第一次工作

[英]While loop malfunction, random integrer only works first time

此代碼應該生成一個隨機數,然后查看該位置是否已被占用或可用,它第一次工作正常,但是當它的機器人第二輪時,它只是垃圾郵件錯誤:您已經放置在這里,再試一次. 我正在為學校制作一個連續 3 個機器人,但我被卡住了。

    static int EasyMode(int plats1, int plats2, int plats3, int plats4, int plats5, int plats6, 
    int plats7, int plats8, int plats9) {
    DateTime dt = DateTime.Now;
    int random = dt.Millisecond;
    random = dt.Millisecond;
    random = (random / 100);
    bool finished = false;
    while (finished == false) {
        random = dt.Millisecond;
        random = (random / 100);
        if (random == 0) {
            random = dt.Millisecond;
            random = (random / 100);
        }
        else if (random == 10) {
            random = dt.Millisecond;
            random = (random / 100);
        }
        else {
            switch (random) {
                case 1:
                    if (plats1 == 1) {
                        Console.WriteLine("Error: You've already placed on here, try again.");
                        continue;
                    }
                    else if (plats1 == 2) {
                        Console.WriteLine("Error: This spot is taken by the bot");
                        continue;
                    }
                    else {
                        Console.WriteLine("Success");
                        finished = true;
                        return 1;
                    }
                case 2:
                    if (plats2 == 1) {
                        Console.WriteLine("Error: You've already placed on here, try again.");
                        continue;
                    }
                    else if (plats2 == 2) {
                        Console.WriteLine("Error: This spot is taken by the bot");
                        continue;
                    }
                    else {
                        Console.WriteLine("Success");
                        finished = true;
                        return 2;
                    }
                case 3:
                    if (plats3 == 1) {
                        Console.WriteLine("Error: You've already placed on here, try again.");
                        continue;
                    }
                    else if (plats3 == 2) {
                        Console.WriteLine("Error: This spot is taken by the bot");
                        continue;
                    }
                    else {
                        Console.WriteLine("Success");
                        finished = true;
                        return 3;
                    }
                case 4:
                    if (plats4 == 1) {
                        Console.WriteLine("Error: You've already placed on here, try again.");
                        continue;
                    }
                    else if (plats4 == 2) {
                        Console.WriteLine("Error: This spot is taken by the bot");
                        continue;
                    }
                    else {
                        Console.WriteLine("Success");
                        finished = true;
                        return 4;
                    }
                case 5:
                    if (plats5 == 1) {
                        Console.WriteLine("Error: You've already placed on here, try again.");
                        continue;
                    }
                    else if (plats5 == 2) {
                        Console.WriteLine("Error: This spot is taken by the bot");
                        continue;
                    }
                    else {
                        Console.WriteLine("Success");
                        finished = true;
                        return 5;
                    }
                case 6:
                    if (plats6 == 1) {
                        Console.WriteLine("Error: You've already placed on here, try again.");
                        continue;
                    }
                    else if (plats6 == 2) {
                        Console.WriteLine("Error: This spot is taken by the bot");
                        continue;
                    }
                    else {
                        Console.WriteLine("Success");
                        finished = true;
                        return 6;
                    }
                case 7:
                    if (plats7 == 1) {
                        Console.WriteLine("Error: You've already placed on here, try again.");
                        continue;
                    }
                    else if (plats7 == 2) {
                        Console.WriteLine("Error: This spot is taken by the bot");
                        continue;
                    }
                    else {
                        Console.WriteLine("Success");
                        finished = true;
                        return 7;
                    }
                case 8:
                    if (plats8 == 1) {
                        Console.WriteLine("Error: You've already placed on here, try again.");
                        continue;
                    }
                    else if (plats8 == 2) {
                        Console.WriteLine("Error: This spot is taken by the bot");
                        continue;
                    }
                    else {
                        Console.WriteLine("Success");
                        finished = true;
                        return 8;
                    }
                case 9:
                    if (plats9 == 1) {
                        Console.WriteLine("Error: You've already placed on here, try again.");
                        continue;
                    }
                    else if (plats9 == 2) {
                        Console.WriteLine("Error: This spot is taken by the bot");
                        continue;
                    }
                    else {
                        Console.WriteLine("Success");
                        finished = true;
                        return 9;
                    }
            }
            continue;
        }
        continue;
    }
    return 69;
}

(我需要更多細節)

如果你需要隨機值,你應該使用Random class。 像這樣:

var rnd = new Random();
int random = rnd.Next(1, 10); // assuming 1 is minimum and 9 is maximum, 
// upper boundary is exclusive 
bool finished = false;
while (finished == false) {
    // generate value here the same way if needed

你的隨機數總是一樣的。 變量dt不會像您預期的那樣使用DateTime.Now的值進行更新。 該值設置一次(當您分配它時)到當前時間。

為了保持當前機制的完整性,您需要繼續查詢DateTime.Now.Millisecond (而不是dt

while(!finished)
{
     int random = DateTime.Now.Millisecond / 100;
     // do stuff with random value
     // if zero or ten, continue loop go get new value
     if (random == 0 || random == 10) {
         continue;
     } else {
         // etc 
     } 
}

但請注意,在這樣的循環中, DateTime.Millisecond並不是最好的選擇。 由於其精度和此循環的性質,許多連續的迭代可能最終得到相同的值。 加上它本身並不是最好的隨機數生成器。

真正應該做的是使用專門為此目的的框架 class - Random - 獲取 0 到 99 之間的值

var rand = new Random(); // outside of loop
while(!finished)
{
     var random = rand.Next(0, 100);
     // do stuff with random value
     // if zero or ten, continue loop go get new value
     if (random == 0 || random == 10) {
         continue;
     } else {
         // etc 
     } 
}

另請注意,您似乎確實想要介於 0-10(或者更確切地說,包括 1-9)之間的數字,在這種情況下,您可以使用以下任何一種:

使用Random並正確設置邊界:

int random = rand.Next(1, 10); // 1-9

使用Millisecond模 10:

int random = DateTime.Now.Millisecond % 10; // 0-9,  still need to test its not zero

使用Millisecond模 9:

int random = (DateTime.Now.Millisecond % 9) + 1; // 1-9

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM