English 中文(简体)
全球阵列没有得到正确利用
原标题:Global array not being accessed correctly
  • 时间:2012-05-20 22:06:24
  •  标签:
  • c#

以下是我的现行法典:

    namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public int[] trialArray = new int[10];
        public int trialCounter = -1;

        private void button1_Click(object sender, EventArgs e)
        {
            bool button1Click = true;
            if (button1Click == true) 
            {
                ITIpanel.Visible = true;   

                for (int i = 0; i < trialArray.Length; i++) { trialArray[i] = -1; } // Set default value through array

                int counter = 0;

                Random rnd = new Random();

                while (counter < 10 / 2)
                {  // Red trials, fill half array

                    int index = rnd.Next(0, 10 - 1);

                    if (trialArray[index] == -1) { trialArray[index] = 1; ++counter; } //if unchanged value, change it

                }
                while (counter < 10)
                {
                    int index = rnd.Next(0, 10);

                    if (trialArray[index] == -1) { trialArray[index] = 2; ++counter; }
                }
            }
        }

        private void ITIpanel_Paint(object sender, PaintEventArgs e)
        {
            if (ITIpanel.Visible == true)
            {
                trialCounter += 1; 
                timer1.Enabled = true;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {

            ITIpanel.Visible = false;
            timer1.Enabled = false; 
            if (trialArray[trialCounter] == 1) { redstimPanel.Visible = true;  }

            else { bluestimPanel.Visible = true;}

            if (trialCounter == 9) { Application.Exit(); } 


        }
        public int counter = 0;
        public event EventHandler Clicked5TimesEvent;
        private void OnClicked5TimesEvent()
        { if (Clicked5TimesEvent != null) { Clicked5TimesEvent(this, EventArgs.Empty); } }

        private void bluestimPanel_MouseDown(object sender, EventArgs e)
        {
            //FR requirement
            counter++; if (counter % 5 == 0) { redstimPanel.Visible = false; ITIpanel.Visible = true; }
        }

        private void redstimPanel_MouseDown(object sender, EventArgs e)
        {
            //FR requirement
            counter++; if (counter % 5 == 0) { redstimPanel.Visible = false; ITIpanel.Visible = true; }
        }


    }
}

如你所知,我正试图建立一个有10个项目的全球阵列。 在纽扣区,10件物品应作改动,其中一半含有价值1,另一半含有价值2。

然后,根据时间尺度,根据<代码> EarthCounter中确定出入阵列部分的价值,应当显示redstimPanelbluestimPanel

因此,如果审判员等于8人,而审判员的8人等于1人,那么红利小组就应当变得可以辨认。 或者,如果在审判中,8人等于2人,那么蓝盔部队就应当变得可以辨认。

然而,这不符合我所希望的那样。 因此,我的准则显然有一些问题。 各位是否有任何建议?

问题回答

你们永远不会重新打着反面,或者有第二处(两处)。

随机编号、编号(a,b) a-下限(包括),b-上限(排他性)。 因此,它应当排在后面。

while (counter < 10 / 2) { // Red trials, fill half array
    int index = rnd.Next(0, 10);

    if (trialArray[index] == -1) { trialArray[index] = 1; ++counter; } //if unchanged value, change it
}
//Counter on the first loop here is already 5 (it exited the previous loop)
//So allow it to get to 10, and populate the FULL array.
while (counter < 10) {
    int index = rnd.Next(0, 10);

    if (trialArray[index] == -1) { trialArray[index] = 2; ++counter; }
}

请允许我就您的法典向你提出一些建议和一些解释:

首先,你可能希望当地纽州1 Click变量,以了解 but是否被点击。 为此,您应将其置于这一职能之外,否则它就永远不会被利用,而且每点击,情况就是如此:

bool button1Click = false;
private void button1_Click(object sender, EventArgs e)
{                
    if (!button1Click)
    {
  1. 如果你有某种条件,你就希望守则能够决定,表达是真实的还是虚假的,你可能会忽略“=真实的”部分,因为它没有增加任何新的内容。

  2. 你有两任。 你的想法是,将反面控制到5年,先是一部法典,然后是从5到10部第二部法典。 现在,请允许我解释一下实际上正在做些什么。 反射将持续到5个随机指数填充的1个。 之后,在5岁时,这一表述将变成虚假的,并且脱离 lo。 自第二次发言以来,它只是避而不谈。 很多解决办法之一,就是在这样的情况下:

    while (counter < 10) { if (counter<5) { // fill red } else { // fill blue } }

  3. The way you fill up the values in your array. Have you thought about what s going to happen when the same index will be generated several times? It means it ll overwrite the previous value while certain index will remain -1.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签