C#范围或类似错误?出现指定序列的预期硬币投掷数

时间:2015-11-27 22:00:45

标签: c#

该程序计算预期的成本,直到给定的序列出现。该公式对2的幂进行求和,其中幂是序列的前k个元素与最后k个元素匹配的k。

我的问题是,当我用两个不同的序列测试它时,它只返回一次结果两次。我假设它正在完成它的样子并且在test1被实例化时用test4覆盖test4,但是这段代码看起来与类似于没有的小练习的代码相似有这种覆盖行为。

这是我的第二个编程课程,我在C#中的第一个编程课程,都没有用我的母语,所以我对某些概念可能会有点慢。 我怀疑这与public static...(我不知道如何引用它们)或可能是保护级别的其中一行有关。 test1的期望值应为38(2 + 2 ^ 2 + 2 ^ 5)。

using System;
namespace CSProject
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Expectation tests");
            PlayerSequence test1 = new PlayerSequence("bc00100");
            PlayerSequence test4 = new PlayerSequence("101101");

            Console.WriteLine("Expectation of test1 bc00100");
            Console.WriteLine (test1.Expectation ());
            Console.WriteLine("Expectation of test4 101101");
            Console.WriteLine(test4.Expectation());
        }
    }

    class PlayerSequence
    {
        //ATTRIBUTES
        private static bool[] sequence;

        //CONSTRUCTORS
        public PlayerSequence()
        {
        }

        public PlayerSequence(string sequence_String)//Seems to work!!
            {
                char[] sequence_array = sequence_String.ToCharArray();
                int inputLength = sequence_array.Length;
                int sequence_length = 0;
                for( int i = 0; i < inputLength; i++) {
                    if (sequence_array[i] == '1' || sequence_array[i] == '0') {
                        sequence_length++;
                    }
                }
                sequence = new bool[sequence_length];///KEYItem
                int input_index_adjustment = 0;
                for (int i = 0; i < inputLength; i++) {
                    int sVal;
                    if (!Int32.TryParse(sequence_String[i].ToString(), out sVal))
                    {
                        input_index_adjustment++;
                        continue;
                    }
                    if (sVal == (Int32)1)
                        PlayerSequence.sequence[i - input_index_adjustment] = true;
                    if (sVal == (Int32)0)
                        PlayerSequence.sequence[i - input_index_adjustment] = false;
                    if(sVal != 1 && sVal != 0)
                        input_index_adjustment++;
                }
            }

        public override string ToString()//Works
        {
            string retString;
            retString = "";
            for (int i = 0; i < sequence.Length;i++) {
                if (sequence[i] == true) retString = retString + "T ";
                else retString = retString + "H ";
            }
            return retString;
        }

        public ulong Expectation(){
            ulong espTA = 0;
            for (int kexp = 0; kexp < /*PlayerSequence.*/sequence.Length; kesp++)
            {
                if(SeqCheck(sequence,kesp+1))
                    expTA = expTA + (ulong)Math.Pow(2, kexp+1);
            }
            return espTA;
        }//end Expectation

        public static bool SeqCheck(bool[] toCheck, int k){
            //Test of required property for each power of 2 here k
            int a = toCheck.Length ;
            bool seqgood = false;
            bool[] checkStart = new bool[k];
            bool[] checkEnd = new bool[k];
            for (int i = 0; i < k; i++) {//loop sets up subarrays to compare
                checkStart[i] = toCheck[i];
                checkEnd[i] = toCheck[a - k + i];
            }
            for (int i = 0; i < k; i++){//loop does comparison
                if(checkStart[i] != checkEnd[i])
                {
                    seqgood = false;
                    break;
                }
                seqgood = true;
            }
            return seqgood;
        }//end SeqCheck   
    }//end PlayerSequence class
}//End this section of the namespace

1 个答案:

答案 0 :(得分:2)

您在类中使用static关键字作为局部变量。通过这样做,您可以将变量作为类型的一部分(PlayerSequence)而不是PlayerSequence的实例(test1,test4)。以下是在我的机器上工作。

class PlayerSequence
{
    //ATTRIBUTES
    private bool[] sequence;

    //CONSTRUCTORS
    public PlayerSequence()
    {
    }

    public PlayerSequence(string sequence_String)//Seems to work!!
    {
        char[] sequence_array = sequence_String.ToCharArray();
        int inputLength = sequence_array.Length;
        int sequence_length = 0;
        for (int i = 0; i < inputLength; i++)
        {
            if (sequence_array[i] == '1' || sequence_array[i] == '0')
            {
                sequence_length++;
            }
        }
        sequence = new bool[sequence_length];///KEYItem
        int input_index_adjustment = 0;
        for (int i = 0; i < inputLength; i++)
        {
            int sVal;
            if (!Int32.TryParse(sequence_String[i].ToString(), out sVal))
            {
                input_index_adjustment++;
                continue;
            }
            if (sVal == (Int32)1)
                sequence[i - input_index_adjustment] = true;
            if (sVal == (Int32)0)
                sequence[i - input_index_adjustment] = false;
            if (sVal != 1 && sVal != 0)
                input_index_adjustment++;
        }
    }

    public override string ToString()//Works
    {
        string retString;
        retString = "";
        for (int i = 0; i < sequence.Length; i++)
        {
            if (sequence[i] == true) retString = retString + "T ";
            else retString = retString + "H ";
        }
        return retString;
    }

    public ulong Expectation()
    {
        ulong espTA = 0;
        for (int kexp = 0; kexp < sequence.Length; kexp++)
        {
            if (SeqCheck(sequence, kexp + 1))
                espTA = espTA + (ulong)Math.Pow(2, kexp + 1);
        }
        return espTA;
    }//end Expectation

    public bool SeqCheck(bool[] toCheck, int k)
    {
        //Test of required property for each power of 2 here k
        int a = toCheck.Length;
        bool seqgood = false;
        bool[] checkStart = new bool[k];
        bool[] checkEnd = new bool[k];
        for (int i = 0; i < k; i++)
        {//loop sets up subarrays to compare
            checkStart[i] = toCheck[i];
            checkEnd[i] = toCheck[a - k + i];
        }
        for (int i = 0; i < k; i++)
        {//loop does comparison
            if (checkStart[i] != checkEnd[i])
            {
                seqgood = false;
                break;
            }
            seqgood = true;
        }
        return seqgood;
    }//end SeqCheck   
}