英文:
C# changed specific value int and looping
问题
I changed the question sample, I hope everyone understands what I meant.
I'm using .NET WinForms App.
I want to make label1.Text show "1->2->3" and then go back to 1, like repeating or looping through 1, 2, 3.
I tried this sample:
public void test(int value)
{
label1.Text = value.ToString();
}
public void Timer1(object sender, EventArgs e)
{
int checkvalue = 0;
int A = 1; int B = 2; int C = 3;
if (checkvalue == 0) { test(A); checkvalue = 1; }
if (checkvalue == 1) { test(B); checkvalue = 2; }
if (checkvalue == 2) { test(C); checkvalue = 0; }
}
But label1.Text only shows 3.
英文:
i changed the question sample, i hope everyone understand what i meant.
i'm use .NET WinformApp
i want to make label1.Text gonna be show 1->2->3 and back to 1 again like repeat or looping from 1,2,3
i try this sample :
public void test(int value)
{
label1.Text = value.ToString();
}
public void Timer1(object sender, EventArgs e)
{
int checkvalue = 0;
int A = 1; int B = 2; int C = 3;
if (checkvalue == 0) { test(A); checkvalue = 1; }
if (checkvalue == 1) { test(B); checkvalue = 2; }
if (checkvalue == 2) { test(C); checkvalue = 0; }
}
but label1.Text just only show 3
答案1
得分: 0
在这行代码中:
if (checkvalue == 0){ test(A); checkvalue == 1;}
仔细看最后一条语句:
checkvalue == 1;
它看起来像一个赋值表达式还是一个相等比较?
英文:
In this line:
if (checkvalue == 0){ test(A); checkvalue == 1;}
Look carefully at the final statement:
checkvalue == 1;
Does it look like an assignment expression, or an equality comparison?
答案2
得分: 0
目前,checkvalue
和值 A
、B
和 C
是局部变量。换句话说:它们在调用 Timer1
方法时创建,在 Timer1
方法结束时消失。整个 Timer1
方法的执行时间比眨眼间还要短,所以你的所有逻辑几乎同时执行。这意味着到 Timer1
结束时,test
已经为 A
、B
和 C
调用过,而 checkvalue
已经循环从 0、1、2 到 0。
你需要做的是在类内部定义字段变量:
public class MyFormClass : Form // 这个已经存在于你的代码中,但名称可能不同
{
private int checkvalue = 0;
// A、B 和 C 代表什么不太清楚,但如果它们应该能够在 Timer1 调用之间更改,你可能也希望将它们设为字段变量或者属性
public void test(int value)
{
label1.Text = value.ToString();
}
接下来要解决的问题是你的 if
语句。在评估第一个 if 语句并执行其主体后,下一个 if 语句也将被执行,因为你现在已经将 checkvalue
设置为正确的值以通过布尔表达式的评估。因此,我们应该将其更改为 if/else if/else
模式,以便在 Timer1
执行期间每次只调用一次 test
:
public void Timer1(object sender, EventArgs e)
{
int A = 1; int B = 2; int C = 3;
if (checkvalue == 0)
{
test(A);
checkvalue = 1;
}
else if (checkvalue == 1)
{
test(B);
checkvalue = 2;
}
else
{
test(C);
checkvalue = 0;
}
}
这样应该实现你期望的逻辑,循环访问 A、B 和 C。
另一种方法是使用数组并跟踪位置来完成。这样你可以添加更多项目而无需添加更多的 if/else if/else 语句:
public class MyFormClass : Form
{
private int position = 0;
private int[] values = new int[] { 1, 2, 3 };
public void test(int value)
{
label1.Text = value.ToString();
}
public void Timer1(object sender, EventArgs e)
{
test(values[position]);
// 增加位置并在超出数组末尾时重置为 0
if (++position >= values.Length)
{
position = 0;
}
}
}
英文:
As it stands, checkvalue
and the values A
, B
, and C
are local variables. That is to say: they are created when the Timer1
method is called, and cease to exist when the Timer1
method ends. The entire Timer1
method will execute within less time than the blink of an eye, so all of your logic will execute almost at once. This means that by the end of Timer1
, test
has been called for A
, B
, and C
, and checkvalue
has cycled through 0, 1, 2, and back to 0.
What you need to do is define field variables within the class instead:
public class MyFormClass : Form // this already exists in your code, but the name might be different
{
private int checkvalue = 0;
// It's not clear what A, B, and C reprsent, but if they should be able to
// change between calls to Timer1, you may also want these to be field
// variables or perhaps properties
public void test(int value)
{
label1.Text = value.ToString();
}
The next problem to address is your if
statements. After the first one is evaluated and its body executed, the next one will also be executed because you've now set checkvalue
to the correct value for the evaluation of the boolean expression to pass. We should therefore change it to an if/else if/else
pattern so that only one call to test
will occur per execution of Timer1
:
public void Timer1(object sender, EventArgs e)
{
int A = 1; int B = 2; int C = 3;
if (checkvalue == 0)
{
test(A);
checkvalue = 1;
}
else if (checkvalue == 1)
{
test(B);
checkvalue = 2;
}
else
{
test(C);
checkvalue = 0;
}
}
This should achieve your desired logic of looping across A, B, and C over and over.
Another way to do this might be to use an array and a track the position. That way you could add more items without adding more if/else if/else statements:
public class MyFormClass : Form
{
private int position = 0;
private int[] values = new int[] { 1, 2, 3 };
public void test(int value)
{
label1.Text = value.ToString();
}
public void Timer1(object sender, EventArgs e)
{
test(values[position]);
// Increment position and reset to 0 if it goes beyond
// the end of the array
if (++position >= values.Length)
{
position = 0;
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论