简体   繁体   English

While循环和条件C#

[英]While Loop And Condition C#

I'm having trouble with something that should seem so simple. 我在看似简单的事情上遇到了麻烦。 I'm using a conditional and on a pretest loop While and it doesn't seem to even execute one because the conditions are met but they're not. 我在条件循环和预测试循环上使用While,它似乎甚至没有执行一个循环,因为条件已满足,但条件却未满足。

I have but the loop never seems be met/ when I break on it. 我有,但是循环似乎永远无法实现/当我中断它时。 It just skips over it 它只是跳过它

int sorter = random.Next(0, 10);
bool player1full = false;
bool player2full = false;

while (player1full && player2full == false)
{
    if (chuckcards[sorter] != null)
    {
         while (player1full != true)
         {
              if (player1.Count != 5)
              {
                   player1.Enqueue(chuckcards[sorter]);
                   chuckcards[sorter] = null;
              }
              else
              {
                   player1full = true;
              }

              sorter = random.Next(0, 10);
         }

         while (player2full != true)
         {
              if (chuckcards[sorter] != null)
              {
                   if (player2.Count != 5)
                   {
                        player2.Enqueue(chuckcards[sorter]);
                        chuckcards[sorter] = null;
                   }
                   else
                   {
                        player2full = true;
                   }

                   sorter = random.Next(0, 10);
               }
          }
     }
     else
     {
          sorter = random.Next(0, 10);
     }
}

My logic maybe slightly off and I'm just wanting someone to point me in the right direction/see my error. 我的逻辑可能略有偏离,我只是希望有人指出我正确的方向/查看我的错误。

Thank you 谢谢

It will never enter the loop because here: 它永远不会进入循环,因为在这里:

bool player1full = false;
bool player2full = false;
while (player1full && player2full == false)

This will test the Boolean value of player1full , and if it's true , then test the Boolean value of player2full == false . 这将测试player1full的布尔值,如果为true ,则测试player2full == false的布尔值。 Since player1full is false , it stops right there and never enters the loop. 由于player1fullfalse ,它就在那里停止并且永远不会进入循环。 I think what you want is: 我认为您想要的是:

while (player1full == false && player2full == false)

Or equivalently 或同等

while (!player1full && !player2full)

Or even (by De Morgan's Law ): 甚至(根据德摩根定律 ):

while (!(player1full || player2full))

However, it seems like the entire outer loop is unnecessary. 但是,似乎不需要整个外部循环。 I can't be entirely sure without known the full context of your program (and that's out of scope for this question), but it could be rewritten as: 我无法完全确定是否知道程序的完整上下文(这超出了此问题的范围),但可以将其重写为:

int sorter;
while (player1.Count != 5)
{
    sorter = random.Next(0, 10);
    if (chuckcards[sorter] != null)
    {
        player1.Enqueue(chuckcards[sorter]);
        chuckcards[sorter] = null;
    }
}

while (player2.Count != 5)
{
    sorter = random.Next(0, 10);
    if (chuckcards[sorter] != null)
    {
        player2.Enqueue(chuckcards[sorter]);
        chuckcards[sorter] = null;
    }
}

The problem is here: 问题在这里:

bool player1full = false;
bool player2full = false;
while (player1full && player2full == false)

that is not equivalent to 那不等于

while(player1full == false && player2full == false)

the == operator results in a new boolean, so thus the following is valid: ==运算符会产生一个新的布尔值,因此以下内容有效:

bool myBool = num1 == num2

What your condition essentially breaks down to is 您的病情基本上可以归结为

(false && (false == false))

which reduces to 减少到

(false && true)

which reduces to 减少到

(false)

It seems that everyone is thinking that you want to loop as long as both are false. 似乎每个人都在想只要两个都是假的就想循环。 But according to the logic, it looks to me that you only want one of them to be not-full. 但是根据逻辑,在我看来,您只希望其中之一不完整。

If that is indeed the case, the condition should be !player1full || !player2full 如果确实如此,则条件应为!player1full || !player2full !player1full || !player2full . !player1full || !player2full

The reason I think that you might want only one to be not-full is that one may be full but the other one still needs handling. 我认为您可能只希望其中一个未满的原因是,一个可能已满,但另一个仍需要处理。 Not sure though. 虽然不确定。 Depends on how you randomly distribute the cards to players, or whatever... And you seem to have edited that part out. 取决于您如何随机地将牌分发给玩家或其他任何人。。。您似乎已经编辑了该部分。

By the way, your shuffling method is horrible. 顺便说一句,您的改组方法太可怕了。 Here is a neat and simple example: 这是一个简洁的示例:

List<string> cards = new List<string> { "1", "2", "3", ... , "10", "J", "Q", "K" };
List<string> shuffled = cards.Select(x => new { X = x, Y = random.Next() })
                             .OrderBy(x => x.Y)
                             .Select(x => x.X).ToList();

This isn't tested, since I don't have VS now, but the idea is to match every card to a random number, sort according to the random numbers, and then lose the extra information (that random number) so you have a shuffled list of cards. 这没有经过测试,因为我现在没有VS,但是我们的想法是将每张卡匹配一个随机数,根据随机数进行排序,然后丢失额外的信息(该随机数),因此您有了洗牌清单。

So bottom line, you can just have a list of a player's cards, and shuffle it. 因此,最重要的是, 您只需要列出玩家的牌,然后将其洗牌即可。 Or you could have a full deck, shuffle, and take top 5 for each player. 或者,您可以有一个完整的套牌,随机播放,并为每个玩家获得前5名。 Whichever you choose, you want to shuffle them nicely. 无论您选择哪种方式,都希望很好地对其进行洗牌。

Example for dealing cards after full-deck shuffling: 在全牌洗牌后发牌的示例:

for (var i = 0; i < CARDS_PER_PLAYER; i++)
{
    foreach (var player in players)
    {
        // Shouldn't happen, if code is carefully planned
        if (cards.Count == 0) throw new ApplicationException("Out of cards!");

        // Deal the top card
        player.Enqueue(cards[0]);
        // Remove from deck, doh! Card can't be in deck AND in player's hand anyways
        cards.RemoveAt(0);
    }
}

This will never be true: 这将永远是不正确的:

bool player1full = false;
bool player2full = false;
while (player1full && player2full == false)

Maybe you meant: 也许你的意思是:

while (player1full == false && player2full == false)

You should write : 您应该写:

while(!player1full && !player2full)

good luck! 祝好运!

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

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