【问题标题】:operation on a value not working (operation > array > control)对值的操作不起作用(操作>数组>控制)
【发布时间】:2020-05-21 18:40:18
【问题描述】:
int[] subject = { 0, Maths, English, Construction, IT };  

int t = 1;
int c = 0;

while (t < 4)
{
    if (Convert.ToInt32(Chart[c].Text) != 0)
    {
        if (c < 12)
        {
            c++;
        }                       
        Counter();
    }
    else
    {
        int m = random.Next(1, 4);
        Chart[c].Text = Convert.ToString(m);
        subject[m]--; // this isnt working!!
        Chart[c].Refresh();
        tbTotal--;
    }
}
   private void Counter()
    {
        if(Maths == 0 && math == false)
        {
            math = true;
            t++;
        }
        if (English == 0 && english == false)
        {
            english = true;
            t++;
        }
        if(IT == 0 && it == false)
        {
            it = true;
            t++;
        }
        if(Construction == 0 && construction == false)
        {
            construction = true;
            t++;
        }

没有减去变量“m”以允许 while 循环的转义并限制向文本框添加值。

主语使用不是记录意义,这意味着 if 语句不适用。因为主题永远不会达到 0。

这会创建一个无限的 while 循环并冻结。

【问题讨论】:

  • 你在哪里增加t 因此subject[t] &lt; 4 总是true
  • Counter() 有基本的 if 语句,当数组内的主题变为 0 时会切换和递增,由“subject[m]--;”控制
  • 解释subject[t] AKA subject[1] 将如何大于 4?
  • while (t &lt; 4) 永远不会离开循环,因为t 总是等于 1。
  • 您的代码令人沮丧。 t的范围是什么?看起来它只属于带有while循环的代码,但它神奇地重新出现在Counter函数中。 “数学、英语、建筑、IT”的价值是什么?要获得好的帮助,您需要发布可以为我们复制问题的代码。

标签: c# arrays winforms while-loop


【解决方案1】:

您似乎期待这一行:

subject[m]--; // this isnt working!!

更改用于初始化数组的原始变量MathEnglishConstructionIT,以便稍后您将获得与@987654327中的if()条件块不同的结果@method 查看那些变量。

那不会发生。 subject 数组中的项目只是值。一旦分配给数组,就不再与变量有任何关系了。

您可以通过此代码示例看到它的效果:

int foo = 1;
int[] bar = {foo};

Console.WriteLine(bar[0]); // 1 -- initialing the value worked

bar[0] = 2;
Console.WriteLine(foo); // still 1 -- changing the array doesn't change the variable

foo = 3; 
Console.WriteLine(bar[0]); // still 2 -- changing the variable doesn't change the array

如果整数 where 对象的属性,您对同一对象有引用,这可能会有所不同。然后更改属性会更改两个变量,因为它们引用了同一个对象。但是像整数这样的简单值类型不能这样工作。

相反,我可以这样定义我的数组:

const int MATH = 0;
const int ENGLISH = 1;
const int CONSTRUCTION = 2;
const int IT = 3;

int[] subjects = new int[4];

现在总是使用如下模式引用值:

subjects[MATH] = ...;

或者你可以像这样定义一个类类型:

public class Subject
{
    public int Value {get;set;} = 0;
    public bool Dirty {get;set;} = false;
}

那么您的代码可能会更改为使用这样的新类:

//change old variables to use the new type
var Maths = new Subject();
var English = new Subject();
var Construction = new Subject();
var IT = new Subject();

Subject[] subjects = {null, Maths, English, Construction, IT};

int t = 1;
int c = 0;

while (t < 4)
{
    if (Convert.ToInt32(Chart[c].Text) != 0)
    {
        if (c < 12)
        {
            c++;
        }                       
        Counter();
    }
    else
    {
        int m = random.Next(1, 4);
        Chart[c].Text = Convert.ToString(m);
        subject[m].Value--; 
        Chart[c].Refresh();
        tbTotal--;
    }
}

private void Counter()
{
    if(Maths.Value == 0 && !Maths.Dirty)
    {
        Maths.Dirty = true;
        t++;
    }
    if (English.Value == 0 && !English.Dirty)
    {
        English.Dirty = true;
        t++;
    }
    if(IT.Value == 0 && !IT.Dirty)
    {
        IT.Dirty = true;
        t++;
    }
    if(Construction.Value == 0 && !Construction.Dirty)
    {
        Construction.Dirty = true;
        t++;
    }
}

更好的是,由于这些都是对相同变量的引用,具有相同的类型,您可以进一步更改代码,如下所示:

var Maths = new Subject();
var English = new Subject();
var Construction = new Subject();
var IT = new Subject();

Subject[] subjects = {null, Maths, English, Construction, IT};

int t = 1;
int c = 0;

while (t < 4)
{
    if (Convert.ToInt32(Chart[c].Text) != 0)
    {
        if (c < 12)
        {
            c++;
        }                       
        Counter(subjects);
    }
    else
    {
        int m = random.Next(1, 4);
        Chart[c].Text = Convert.ToString(m);
        subject[m].Value--; 
        Chart[c].Refresh();
        tbTotal--;
    }
}

private void Counter(IEnumerable<Subject> subjects)
{
    foreach(var s in subjects.Where(s => s is object))
    {
       if (s.Value == 0 && !s.Dirty)
       {
           s.Dirty = true;
           t++;
       {
    }
}

【讨论】:

  • 感谢您的帮助,不过我可能只是更改了 while 循环的退出条件。更改最后一个图表文本框后退出,它更混乱、更不灵活但更简单。
猜你喜欢
  • 2017-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-24
  • 1970-01-01
相关资源
最近更新 更多