c# - 对值的操作不起作用(操作>数组>控制)
问题描述
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 循环的转义并限制向文本框添加值。
主语使用不是记录意义,这意味着如果语句不适用。因为主题永远不会达到0。
这会创建一个无限的 while 循环并冻结。
解决方案
您似乎期望这一行:
subject[m]--; // this isnt working!!
更改原始变量Math
, English
,Construction
和IT
用于初始化数组,以便稍后您将在查看这些变量if()
的方法中的条件块中获得不同的结果。Counter()
那不会发生。subject
数组中的项目只是 values。一旦分配给数组,就不再与变量有任何关系。
您可以通过以下代码示例看到它的效果:
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++;
{
}
}
推荐阅读
- angular - 了解如何从 package.json 文件中提取依赖项,以便 Angular 应用程序扫描非捆绑/缩小版本
- css - CSS如何用父填充覆盖子边距?
- git - 在 GitHub 上签入后自动更改文件的最佳实践
- html - 如何更改或更新 angular ag-grid 的主题。我们是否需要编写任何自定义 css 文件或使用除材料之外的任何其他网格
- c - 如何为字符串数组创建指针?
- javascript - 无法读取对象数组上未定义的属性“推送”
- c# - c# 中是否有在整个集合上调用接口方法的快捷方式?
- java - Do-while 循环不会在 switch-case 语句之后循环
- javascript - javascript调用函数然后销毁函数输出
- android - apk 是否包含有效负载,或者如果我想通过 apk 文件共享我的应用程序,我是否必须添加它们?