我不明白为什么这两个代码会给出不同的结果 [英] I dont understand why these two codes are giving different results

查看:66
本文介绍了我不明白为什么这两个代码会给出不同的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

static string[] ranks = new string[13] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };
public static int rankCounter = 0;
public static string[] suits = new string[4] { "♠", "♣", "♦", "♥" };
public static int suitsCounter = 0;
public static int shuffle;
public static Random rnd = new Random();
public static int[] value = new int[52];
public static string numbers = string.Empty;
public static string[] cards = new string[52];
 
static string[] ShuffleCardSystem()
{
    string temp;
    for (int i = 0; i < 52; i++)
    {
        cards[i] = ranks[rankCounter] + suits[suitsCounter];
        rankCounter++;
        if (rankCounter == 13)
        {
            rankCounter = 0;
            suitsCounter += 1;
        }
    }
    for (int i = 51; i >= 0; i--)
    {
        numbers = string.Empty;
        shuffle = rnd.Next(0, i);
        temp = cards[shuffle];
        cards[shuffle] = cards[i];
        cards[i] = temp;
        for (int position = 0; position < cards[i].Length; position++)
        {
            if (char.IsDigit(cards[i][position]))
            {
                numbers += cards[i][position];

                value[i] = Convert.ToInt32(numbers]);
                if (value[i] == 1)
                {
                    value[i] = 11;
                }
            }
            else if (cards[i].Any(char.IsLetter))
            {
                value[i] = 10;
                numbers += cards[i][position];
            }            
        }
    }
}





vs this ...





vs this...

static string[] ShuffleCardSystem()
{
    string temp;
    for (int i = 0; i < 52; i++)
    {
        cards[i] = ranks[rankCounter] + suits[suitsCounter];
        rankCounter++;
        if (rankCounter == 13)
        {
            rankCounter = 0;
            suitsCounter += 1;
        }
    }
    for (int i = 51; i >= 0; i--)
    {
        numbers = string.Empty;
        shuffle = rnd.Next(0, i);
        temp = cards[shuffle];
        cards[shuffle] = cards[i];
        cards[i] = temp;
        for (int position = 0; position < cards[i].Length; position++)
        {
            if (char.IsDigit(cards[i][position]))
            {
                static string[] ShuffleCardSystem()   // WTF ?
                {
                    string temp;
                    for (int i = 0; i < 52; i++)
                    {
                        cards[i] = ranks[rankCounter] + suits[suitsCounter];
                        rankCounter++;
                        if (rankCounter == 13)
                        {
                            rankCounter = 0;
                            suitsCounter += 1;
                        }
                    }
                    for (int i = 51; i >= 0; i--)
                    {
                        numbers = string.Empty;
                        shuffle = rnd.Next(0, i);
                        temp = cards[shuffle];
                        cards[shuffle] = cards[i];
                        cards[i] = temp;
                        for (int position = 0; position < cards[i].Length; position++)
                        {
                            if (char.IsDigit(cards[i][position]))
                            {
                                value[i] = Convert.ToInt32(cards[i][position]);
                                if (value[i] == 1)
                                {
                                    value[i] = 11;
                                }
                            }
                            else if (cards[i].Any(char.IsLetter))
                            {
                                value[i] = 10;
                                numbers += cards[i][position];  
                            }
                        }
                        value[i] = Convert.ToInt32(numbers);
                        if (value[i] == 1)
                        {
                            value[i] = 11;
                        }
                    }
                    else if (cards[i].Any(char.IsLetter))
                    {
                        value[i] = 10;
                    }
                }
            }
        }
    }
}





拥有数字应该无关紧要,因为它等于[i] [位置]



我尝试了什么:



我试图清理我的代码,但不是将1,2,3显示为值,而是给出49,50,51我只想知道为什么



Having "numbers" should not matter because it is equal to [i][position]

What I have tried:

I tried to clean up my codes but instead of showing 1,2,3 as a value it gives 49,50,51 I just want to know why

推荐答案

在一个版本中,您汇编数字中的数字并将其转换为:

In one version, you "assemble" a number in numbers and convert it:
numbers += cards[i][position];
value[i] = Convert.ToInt32(numbers]);

所以每次围绕的位置循环,你转换一个更长的数字。

另一方面,你仍然在数字中组合一个更长的值,但是你不要使用它:

So each time round the position loop, you convert a longer number.
In the other, you still assemble a longer value in numbers, but you don;t use it:

value[i] = Convert.ToInt32(cards[i][position]);



因此,您在 value [i] 中获得的价值将会有所不同。

制作解决方案文件夹的副本,然后打开两个VS实例 - 每个实例一个 - 并通过调试器运行每个版本的代码。如果你逐步完成每个程序,你应该看到你得到的差异,以及为什么相当容易。


So the value you get in value[i] is going to be different.
Make a copy of your solution folder, then open two instances of VS - one for each - and run each version of the code through the debugger. If you step through each program, you should see what differences you get, and why fairly easily.


这篇关于我不明白为什么这两个代码会给出不同的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆